Info I am using abstract models in django every thing is working fine instead of queryset i am try to make query for Domain which is related to the clients model. But i don't understand why Reverse Lookups of Foreign Keys is not working?
TenantMixin && DomainMixin
class TenantMixin(models.Model):
schema_name = models.CharField(max_length=63, unique=True, db_index=True)
class Meta:
abstract = True
class DomainMixin(models.Model):
domain = models.CharField(max_length=253, unique=True, db_index=True)
tenant = models.ForeignKey(settings.TENANT_MODEL, db_index=True, related_name='domains', on_delete=models.CASCADE)
class Meta:
abstract = True
Client
class Client(TenantMixin):
name = models.CharField(max_length=100)
class Domain(DomainMixin):
pass
QuerySet
tenant = Client.objects.all()
for t in tenant:
for i in t.domain_set.all():
print(i)
CodePudding user response:
You set the related_name=…
parameter [Django-doc] to 'domains'
, hence you look up the reverse with:
tenant = Client.objects.all()
for t in tenant:
for i in t.domains.all():
print(i)
Note: There is no need to specify
db_index=True
[Django-doc] for aForeignKey
[Django-doc]. As the documentation specifies: A database index is automatically created on the ForeignKey..