Django model wih a ForeignKey pointing to a salesforce model

216 views Asked by At

I am using django-salesforce and I would like to create a model within Django that has a ForeignKey field pointing to a SFDC model (hosted on force.com).

I created a custom model on force.com, let us call it SFModel, and I can successfully work on it from django (CRUD) by subclassing salesforce.models.Model.

I also created a django.db.models.Model, let us call it DJModel, that has a unique field ForeignKey(SFModel). This model is registered on the admin panel.

All models validate and I can go to my admin panel to try to create a new instance of DJModel. However, when I try to display the create_form in the admin I get the following error :

hasattr(): attribute name must be string

and the debug stream says

enter image description here

So I tried to set an arbitrary alias to the SF entry in the DATABASES of my settings.py. There is a dedicated variable for that :

SALESFORCE_DB_ALIAS = 'youralias'

But I still have the same problem.

Any recommendation?

1

There are 1 answers

0
hynekcer On

Django doesn't support it and an external reference to Salesforce should be currently saved as a CharField and a reference to other databases as IntegerField.

Django docs about Limitations of multiple databases:

Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.

I tried the cross reference with sqlite as 'default' database. It was possible to create an object of model DJModel with cross-database reference from sqlite to Salesforce. It behaves similarly to normal Django cross-database references, without obscure errors and only a dot reference can be used.

EDIT: Simplified after many years.