2

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?

hynekcer
  • 14,942
  • 6
  • 61
  • 99
Buddyshot
  • 1,614
  • 1
  • 17
  • 44
  • It is similar to support for multiple databases in Django, with standard backends like Postgres or MySQL. You can not create a ForeignKey e.g. from Postgres to MySQL. Even ForeignKeys between different different databases of the same type can not be created. I recommend something similar to [GenericForeignKey](https://docs.djangoproject.com/en/1.7/ref/contrib/contenttypes/#generic-relations). If you find a solution that works between two standard databases, I can help you to find a solution that works between a standard database and Salesforce. – hynekcer Sep 26 '14 at 13:28
  • 1
    I recommend to close this question, because it is a feature unsupported by Django, not be reproduced neither on the current versions or the versions used that time, currently obsoleted. – hynekcer Sep 05 '17 at 18:16

1 Answers1

1

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.

hynekcer
  • 14,942
  • 6
  • 61
  • 99