I know Django does not support foreign keys across multiple databases (originally Django 1.3 docs)
But I\'m looking for a workaround.
Inspired by @Frans ' comment. My workaround is to do this in business layer. In the example given this question. I would set fruit to an IntegerField
on Article
, as "not to do integrity check in data layer".
class Fruit(models.Model):
name = models.CharField()
class Article(models.Model):
fruit = models.IntegerField()
intro = models.TextField()
Then honor reference relation in application code (business layer). Take Django admin for example, in order to display fruit as a choice in Article's add page, you populate a list of choices for fruit manually.
# admin.py in App article
class ArticleAdmin(admin.ModelAdmin):
class ArticleForm(forms.ModelForm):
fields = ['fruit', 'intro']
# populate choices for fruit
choices = [(obj.id, obj.name) for obj in Fruit.objects.all()]
widgets = {
'fruit': forms.Select(choices=choices)}
form = ArticleForm
list_diaplay = ['fruit', 'intro']
Of course you may need to take care of form field validation (integrity check).