Skip to content

Commit

Permalink
Adding django notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Oct 23, 2014
1 parent f3de8a7 commit 46d4e5d
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions django/django_notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Some basic Django operations
# Assumes Django >= 1.7


# Initial DB configuration
python manage.py migrate

# Refreshing DB after changes to models.py (assumes app name = net_system)
python manage.py makemigrations net_system
python manage.py migrate


# Using Django as an ORM will require the following
export DJANGO_SETTINGS_MODULE=djproject.settings
export PYTHONPATH=~/DJANGOX/djproject/


# Example object creation
pynet_rtr1 = NetworkDevice(
device_name = 'pynet-rtr1',
ip_address = '10.10.10.10',
device_class = 'cisco_ios_onepk',
api_port = 15002,
)
pynet_rtr1.save()

# Example object creation using get_or_create
cisco_creds = Credentials.objects.get_or_create(
username = 'username',
password = '********',
description = 'Cisco router credentials'
)


# Examples retrieving objects
$ python manage.py shell
>>> from net_system.models import NetworkDevice
>>> net_devices = NetworkDevice.objects.all()
>>> print net_devices
[<NetworkDevice: pynet-rtr1>,
<NetworkDevice: pynet-rtr2>,
<NetworkDevice: pynet-sw1>,
<NetworkDevice: pynet-sw2>,
<NetworkDevice: pynet-sw3>,
<NetworkDevice: pynet-sw4>]

>>> a_device = NetworkDevice.objects.get(device_name='pynet-rtr1')
>>> a_device
<NetworkDevice: pynet-rtr1>

# Example modifying an object
>>> a_device.vendor = 'cisco'
>>> a_device.vendor
'cisco'
>>> a_device.save()

# Example delete object
>>> a_device.delete()


# Example retrieving all related objects for a foreign-key relationship
>>> cisco_creds.networkdevice_set.all()
[<NetworkDevice: pynet-rtr1>, <NetworkDevice: pynet-rtr2>]

0 comments on commit 46d4e5d

Please sign in to comment.