ModelForm including ForeignKeys with multiple databases

Having two models in models.py such as:

    provider_name = models.TextField(primary_key=True, default=None)
    provider_address = models.TextField(null=False, blank=False, default=None)

class PhoneNumber(models.Model):
    number = models.IntegerField(primary_key=True, default=None)
    provider = models.ForeignKey(PhoneProvider, on_delete=models.SET_NULL, default=None,
                                 null=False, blank=False)

And then in forms.py I create two model forms:

    class Meta:
        model = PhoneProvider
        fields = "__all__"

class FormPhoneNumber(forms.ModelForm):
    class Meta:
        model = PhoneNumber
        fields = "__all__"

My question is, how to use the FormPhoneNumber (which has a ForeignKey) when using multiple databases with manual routing. Do I need to overwrite the field employing a query employing .using('database_name')?

I am aware that Django does not support cross-referencing of Foreign keys along multiple databases, but I am talking about two tables placed in the same database. How can I let the ModelForm now the database from which it can extract the foreign key options?

Back to Top