Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python_wiki:django_api [2018/08/04 15:12] billdozor [Load Objects] |
python_wiki:django_api [2019/05/25 23:50] (current) |
||
---|---|---|---|
Line 4: | Line 4: | ||
Interacting with the Django Python API. | Interacting with the Django Python API. | ||
+ | |||
+ | API Reference: https:// | ||
**Checklist** | **Checklist** | ||
Line 11: | Line 13: | ||
---- | ---- | ||
- | ====== 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 49: | Line 51: | ||
Objects can be retrieved just like in views.py. | Objects can be retrieved just like in views.py. | ||
+ | |||
+ | Field lookup reference: https:// | ||
\\ | \\ | ||
Line 59: | Line 63: | ||
for node in asset_list: | for node in asset_list: | ||
print(node.asset_name) | print(node.asset_name) | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | **Load Multiple Objects that Match a filter** | ||
+ | <code python> | ||
+ | # Load with a filter on device type (get all servers) | ||
+ | asset_list = AssetEntry.objects.filter(asset_type__iexact=" | ||
+ | |||
+ | # Print all retrieved objects | ||
+ | for node in asset_list: | ||
+ | print(" | ||
+ | print(" | ||
</ | </ | ||
\\ | \\ | ||
**Load Single Object** | **Load Single Object** | ||
- | < | + | < |
# Load single object | # Load single object | ||
single_asset = AssetEntry.objects.get(asset_name__iexact=" | single_asset = AssetEntry.objects.get(asset_name__iexact=" | ||
Line 74: | Line 90: | ||
Create a new object and then save it to the database. | Create a new object and then save it to the database. | ||
+ | |||
+ | \\ | ||
+ | **Two Steps: Create object and then save it to the database** | ||
<code python> | <code python> | ||
# Set variables that will be used to create a new object | # Set variables that will be used to create a new object | ||
Line 92: | Line 111: | ||
except: | except: | ||
print(" | 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, | ||
</ | </ | ||
Line 120: | Line 154: | ||
===== Delete Objects ==== | ===== Delete Objects ==== | ||
- | FIXME - Fill out how to delete an object. | + | Deleting objects using the API. |
+ | <code python> | ||
+ | # Retrieve a single | ||
+ | single_asset = AssetEntry.objects.get(asset_name__iexact=" | ||
+ | |||
+ | # Delete the object | ||
+ | single_asset.delete() | ||
+ | </ | ||
---- | ---- | ||