Install, configure a Salesforce connection, create a Salesforce model and run.
Install django-salesforce: pip install django-salesforce
Add a salesforce connection to your DATABASES setting:
'salesforce': {
'ENGINE': 'salesforce.backend',
'CONSUMER_KEY': '', # 'client_id' in OAuth2 terminology
'CONSUMER_SECRET': '', # 'client_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 Classic > Setup > App Setup > Create > Apps > Connected apps >
New.
or SalesForce Lightning > Setup > Apps > App Manager > New Connected App.
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. (“Callback URL” should be a safe URL
that maybe doesn’t exist, but is under your control and doesn’t redirect,
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 occurs in a request to Salesforce, review the received error message
that is exactly copied between braces {...} from the
Salesforce response to a Python exception to assist debugging.
See also: Information on settings up Salesforce connected apps
if necessary.
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.
Introspection by inspectdb doesn’t require any object permissions.
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. The full models file is about 2 MB with 500 models
and the export takes 2 minutes, but it is a valid models module that works without
modification. The output of command inspectdb can be restricted by a list
of table_names on the command line, but also ForeignKey fields to omitted models
must be pruned to get a valid complete small model.
(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.
It 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 models
and you can register a minimalistic admin for all omitted Admin classes:
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 a sandbox, before
hand-writing all admin classes. (Foreign keys to huge tables in the production
require a customized admin 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.