python_wiki:django_api

Differences

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

Link to this comparison view

Next revision
Previous revision
python_wiki:django_api [2018/08/03 16:48]
billdozor created
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:
 ---- ----
  
-====== Usage ======+====== Imports and Settings ======
  
-FIXME - TO ADD API EXAMPLES HERE.+Imports and settings to add to your Python script in order to use your Django App's built in API.
  
-Instructions here.+\\ 
 +Import and Settings 
 +<code python> 
 +#!/usr/bin/python 
 + 
 +# Required module for sys.path.append 
 +import sys 
 +# Required module for os.environ 
 +import os 
 + 
 +# Import Django WSGI App Loading capabilities 
 +from django.core.wsgi import get_wsgi_application 
 + 
 +##- Configure Django Settings to point to your App -## 
 +# sys.path = where manage.py is 
 +sys.path.append('/home/django/myprojecthere'
 + 
 +# Settings Module = projectname.settings 
 +os.environ["DJANGO_SETTINGS_MODULE"] = 'myprojecthere.settings' 
 +application = get_wsgi_application() 
 + 
 +# Import Django App Model (from 'myapphere' app) 
 +from myapphere.models import AssetEntry 
 +</code> 
 + 
 +---- 
 + 
 +====== Using Your Apps API ====== 
 + 
 +Different ways to get your app data. 
 + 
 +===== Load Objects ==== 
 + 
 +Objects can be retrieved just like in views.py. 
 + 
 +Field lookup reference: https://docs.djangoproject.com/en/1.11/ref/models/querysets/#field-lookups 
 + 
 +\\ 
 +**Load All Objects** 
 +<code python> 
 +# Load all assets (just like in views.py) 
 +asset_list = AssetEntry.objects.order_by('asset_type', 'asset_env', 'asset_name'
 + 
 +# Print all names (see your models.py for available fields) 
 +for node in asset_list: 
 +  print(node.asset_name) 
 +</code> 
 + 
 +\\ 
 +**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="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> 
 + 
 +\\ 
 +**Load Single Object** 
 +<code python> 
 +# Load single object 
 +single_asset = AssetEntry.objects.get(asset_name__iexact="server99"
 + 
 +# Print the name and os 
 +print("Single Asset: " + single_asset.asset_name + " (OS: " + single_asset.asset_os + ")"
 +</code> 
 + 
 +===== 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 = "server99" 
 +new_type = "Servers" 
 +new_description = "API Added Server" 
 +new_env = "Prod" 
 +new_os = "CentOS 7" 
 +new_hardware = "Virtual" 
 + 
 +# Create the new object 
 +new_entry = AssetEntry(asset_name = new_name, asset_type = new_type, asset_description = new_description, asset_env = new_env, asset_os = new_os, asset_hardware = new_hardware) 
 + 
 +# Attempt to add new object to the database 
 +try: 
 +  new_entry.save() 
 +  print("--> Entry added successfully."
 +except: 
 +  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> 
 + 
 +---- 
 + 
 +===== Change Existing Objects ==== 
 + 
 +Changing an existing object in the database.
 <code bash> <code bash>
-Execution code here+# Load all assets (just like in views.py) 
 +asset_list = AssetEntry.objects.order_by('asset_type', 'asset_env', 'asset_name'
 + 
 +# Change server99's OS to Ubuntu 18.04 
 +for node in asset_list: 
 +  if node.asset_name == "server99": 
 +    node.asset_os = "Ubuntu 18.04" 
 +     
 +    # Attempt to save object change to the database 
 +    try:                                                                                        
 +      node.save() 
 +      print("--> Change saved successfully."
 +    except: 
 +      print("---> ERROR saving change!")
 </code> </code>
  
 ---- ----
 +
 +===== Delete Objects ====
 +
 +Deleting objects using the API.
 +<code python>
 +# Retrieve a single object
 +single_asset = AssetEntry.objects.get(asset_name__iexact="server99")
 +
 +# Delete the object
 +single_asset.delete()
 +</code>
 +
 +----
 +
  
  • python_wiki/django_api.1533329321.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)