-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>] | ||
|