Install django-salesforce: pip install django-salesforce
Add a salesforce connection to your DATABASES setting:
'salesforce': {
'ENGINE': 'salesforce.backend',
'CONSUMER_KEY': '',
'CONSUMER_SECRET': '',
'USER': '',
'PASSWORD': '',
'HOST': 'https://test.salesforce.com',
}
In the example above, all fields should be populated as follows:
CONSUMER_KEY and CONSUMER_SECRET values are for the app used to
connect to your Salesforce account. Instructions for how get these are in
the Salesforce REST API Documentation. Key and secret can be created on
web by:
Salesforce web > Setup > App Setup > Create > Apps > Connected apps >
New.
Click “Enable OAuth Settings” in API, then select “Access and manage
your data (api)” from available OAuth Scopes.
Other red marked fields must be filled, but are not relevant for Django
with password authentication. (For example a “Callback URL” can be an
URL that doesn’t exist, but that is under your control, for the case that
you accidentally activate other OAuth mode later.)
USER is the username used to connect.
PASSWORD is a concatenation of the user’s password and security token.
Security token can be set by My Settings / Personal / Reset My Security Token
or an new token is received by email after every password change.
Security token can be omitted if the local IP address has been
whitelisted in Security Controls / Network Access.
HOST is https://test.salesforce.com to access a sandbox, or
https://login.salesforce.com to access production.
If an error message is received while connecting, review the error received.
Everything in the error message between {...} is exactly copied from the
Salesforce error message to assist debugging.
See also: Information on settings up Salesforce connected apps.
Note about permissions: Everything for a project can work under
restricted Salesforce user account if it has access to objects in your
models. Introspection (inspectdb) doesn’t require any permissions. Running
tests for django_salesforce requires many permissions or Administrator
account for sandbox.
Note about permissions: Administrator rights are only required to run
the full suite of unit tests; otherwise, as long as the account has rights to
read or modify the chosen object, everything should work properly.
Add salesforce.router.ModelRouter to your DATABASE_ROUTERS
setting:
DATABASE_ROUTERS = [
"salesforce.router.ModelRouter"
]
(This is important for switching between ‘salesforce’ database for
models derived from SalesforceModel and ‘default’ database for normal models
with tables created by migrations, especially for ‘django.contrib’.)
Add the salesforce app to your INSTALLED_APPS setting:
INSTALLED_APPS = {
"django.contrib.auth",
"django.contrib.contenttypes",
...
...
"salesforce"
}
(This is necessary for running Salesforce extensions in the command
inspectdb --database=salesforce in development, otherwise it is
not important.)
Define a model that extends salesforce.models.Model (alias SalesforceModel) or export the
complete SF schema by python manage.py inspectdb --database=salesforce
and simplify it to what you need.
(optional) To override the default timeout of 15 seconds,
define SALESFORCE_QUERY_TIMEOUT in your settings file.
It can be one number or better a tuple with a short value for connection
timeout and a longer value that includes time for running a query,
but never need be longer than 30 seconds:
SALESFORCE_QUERY_TIMEOUT = (4, 15) # default (connect timeout, data timeout)
(optional) If you want to use another name for your Salesforce DB
connection, define SALESFORCE_DB_ALIAS in your settings file:
SALESFORCE_DB_ALIAS = 'salesforce' # default
You’re all done! Just use your model like a normal Django model.
(optional) Create a normal Django admin.py module for your Salesforce model:
from salesforce.testrunner.example.universal_admin import register_omitted_classes
# some admin classes that you wrote manually yet
# ...
# end of file
register_omitted_classes(your_application.models)
This is a rudimentary way to verify that every model works in sandbox, before
hand-writing all admin classes. (Foreign keys to huge tables in the production
require customized admins e.g. with search widgets.)
(optional) By default, the Django ORM connects to all DBs at startup. To delay
SFDC connections until they are actually required, define SF_LAZY_CONNECT=True
in your settings file. Be careful when using this setting; since it won’t fail during
the application boot, it’s possible for a bad password to be sent repeatedly,
requiring an account reset to fix.