Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
python_wiki:django_api [2018/08/04 00:06] billdozor [Imports] |
python_wiki:django_api [2018/08/04 15:31] billdozor [Add New Objects] |
||
---|---|---|---|
Line 11: | Line 11: | ||
---- | ---- | ||
- | ====== Imports ====== | + | ====== Imports |
Imports and settings to add to your Python script in order to use your Django App's built in API. | Imports and settings to add to your Python script in order to use your Django App's built in API. | ||
Line 48: | Line 48: | ||
===== Load Objects ==== | ===== Load Objects ==== | ||
- | Objects can now be retrieved just like in views.py. | + | Objects can be retrieved just like in views.py. |
+ | |||
+ | Field lookup reference: https:// | ||
+ | |||
+ | \\ | ||
+ | **Load All Objects** | ||
<code python> | <code python> | ||
# Load all assets (just like in views.py) | # Load all assets (just like in views.py) | ||
asset_list = AssetEntry.objects.order_by(' | asset_list = AssetEntry.objects.order_by(' | ||
- | # Print all names | + | # Print all names (see your models.py for available fields) |
for node in asset_list: | for node in asset_list: | ||
print(node.asset_name) | print(node.asset_name) | ||
</ | </ | ||
- | ===== Save Objects ==== | + | \\ |
+ | **Load Multiple | ||
+ | <code python> | ||
+ | # Load with a filter on device type (get all servers) | ||
+ | asset_list | ||
- | FIXME - To Do | + | # Print all retrieved objects |
+ | for node in asset_list: | ||
+ | print(" | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | **Load Single Object** | ||
+ | <code python> | ||
+ | # Load single object | ||
+ | single_asset = AssetEntry.objects.get(asset_name__iexact=" | ||
+ | |||
+ | # Print the name and os | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | ===== Add New Objects ==== | ||
+ | |||
+ | Create a new object and then save it to the database. | ||
+ | |||
+ | \\ | ||
+ | Two Steps: Create object and then save it to the database | ||
+ | <code python> | ||
+ | # Set variables that will be used to create a new object | ||
+ | new_name = " | ||
+ | new_type = " | ||
+ | new_description = "API Added Server" | ||
+ | new_env = " | ||
+ | new_os = " | ||
+ | new_hardware = " | ||
+ | |||
+ | # Create the new object | ||
+ | new_entry = AssetEntry(asset_name = new_name, asset_type = new_type, asset_description = new_description, | ||
+ | |||
+ | # Attempt to add new object to the database | ||
+ | try: | ||
+ | new_entry.save() | ||
+ | print(" | ||
+ | except: | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | One Step: Create object in the database | ||
+ | <code python> | ||
+ | # Set variables that will be used to create a new object | ||
+ | new_name = " | ||
+ | new_type = " | ||
+ | new_description = "API Added Server" | ||
+ | new_env = " | ||
+ | new_os = " | ||
+ | new_hardware = " | ||
+ | |||
+ | # Create the new object directly | ||
+ | new_entry = AssetEntry.objects.create(asset_name = new_name, asset_type = new_type, asset_description = new_description, | ||
+ | </ | ||
---- | ---- | ||
+ | |||
+ | ===== Change Existing Objects ==== | ||
+ | |||
+ | Changing an existing object in the database. | ||
+ | <code bash> | ||
+ | # Load all assets (just like in views.py) | ||
+ | asset_list = AssetEntry.objects.order_by(' | ||
+ | |||
+ | # Change server99' | ||
+ | for node in asset_list: | ||
+ | if node.asset_name == " | ||
+ | node.asset_os = " | ||
+ | | ||
+ | # Attempt to save object change to the database | ||
+ | try: | ||
+ | node.save() | ||
+ | print(" | ||
+ | except: | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Delete Objects ==== | ||
+ | |||
+ | Deleting objects using the API. | ||
+ | <code python> | ||
+ | # Retrieve a single object | ||
+ | single_asset = AssetEntry.objects.get(asset_name__iexact=" | ||
+ | |||
+ | # Delete the object | ||
+ | single_asset.delete() | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||