Dynamics 365 and Django Integration
Getting an access token
I have used requests. post to get the access token.
Required:
LOGIN_URL: https://login.microsoftonline.com/<TENANT_ID>/oauth2/token
TENANT ID is can be found in Dynamics->Advanced Settings->Customisations -> Developer Resources application
CLIENT_ID: Register an application in Azure AD with user_impersonation permissions (note the Azure AD admin needs to grant access). Client_id is the application id of the app you have registered.
GRANT_TYPE: I have selected grant type as ‘password’
RESOURCE IS the URL of your Dynamics instance, for example, https://<your org>.crm6.dynamics.com
USER_NAME: Your email address to access the resource
Password: Password to your email account
Now to the part when we all of these to get the access token:

import requests and get the access token

Once you have the access token, you can use this token to create, update, and delete entity records in your dynamics instance.
Using Dynamics365crm-python
pip install dynamics365crm-python
from dynamics365crm.client import Client
client = Client(RESOURCE, get_token())
client.set_access_token(get_token())
email =test@abc.com
list_leads = client.get_leads(select=”emailaddress1, leadid”,filter=”emailaddress1 eq ‘{0}’”.format(email), top=’1', orderby=”createdon desc”)
You can perform select, filter, top, orderby operations. Please refer to official documentation for more details.
up_lead =client.update_lead(<id>, emailaddress1 =<email>)
Refer to the official documentation for more operations such as create and delete.
Using Pydynamics
pip install pydynamics
from pydynamics.client import Client as DynamicsClient
from pydynamics.querybuilder import QueryBuilder
API URL: https://<your_org>.crm6.dynamics.com/api/data/v9.0/
dc = DynamicsClient(get_token(), ‘<API url>’)
c_lead =QueryBuilder(‘leads’).data({‘firstname’:’Test’, ‘middlename’:f’{middle_name}’,
‘lastname’:f’{last_name}’,
‘emailaddress1’:f’{email}’,
‘new_dateofbirth’ :f’{dofbirth}’,
‘mobilephone’:f’{phone}’,
‘telephone3’:f’{otherphone}’,})
guid_lead = dc.create(c_lead) //calls the Querybuildr to create the lead.
print(guid_lead) //prints the guid of the lead created
Refer to the official documentation for more operations.
Thank you for your time. Hope this helps.