-
Notifications
You must be signed in to change notification settings - Fork 516
/
Copy pathdjango_notes.txt
64 lines (48 loc) · 1.58 KB
/
django_notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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>]