python_wiki:django_api

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python_wiki:django_api [2018/08/04 15:19]
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://docs.djangoproject.com/en/1.11/ref/models/querysets/
  
 **Checklist** **Checklist**
Line 11: Line 13:
 ---- ----
  
-====== Imports ======+====== Imports and Settings ======
  
 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 66: Line 68:
 **Load Multiple Objects that Match a filter** **Load Multiple Objects that Match a filter**
 <code python> <code python>
 +# Load with a filter on device type (get all servers)
 +asset_list = AssetEntry.objects.filter(asset_type__iexact="servers")
  
 +# Print all retrieved objects
 +for node in asset_list:
 +    print("-> Node Name is: " + node.asset_name)
 +    print("Device Type is: " + node.asset_type)
 </code> </code>
  
Line 82: 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 100: Line 111:
 except: except:
   print("---> ERROR adding entry!")   print("---> ERROR adding entry!")
 +</code>
 +
 +\\
 +**One Step: Create object in the database**
 +<code python>
 +# Set variables that will be used to create a new object
 +new_name = "server99"
 +new_type = "Servers"
 +new_description = "API Added Server"
 +new_env = "Prod"
 +new_os = "CentOS 7"
 +new_hardware = "Virtual"
 +
 +# Create the new object directly
 +new_entry = AssetEntry.objects.create(asset_name = new_name, asset_type = new_type, asset_description = new_description, asset_env = new_env, asset_os = new_os, asset_hardware = new_hardware)
 </code> </code>
  
  • python_wiki/django_api.1533410350.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)