diff --git a/django/django_notes.txt b/django/django_notes.txt new file mode 100644 index 0000000..8fe5b12 --- /dev/null +++ b/django/django_notes.txt @@ -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 +[, + , + , + , + , +] + +>>> a_device = NetworkDevice.objects.get(device_name='pynet-rtr1') +>>> a_device + + +# 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() +[, ] +