I am trying to make a query with related_name. I need to list tenants and its domain. But I am getting this error:
'TenantManager' object has no attribute 'domains'
What am I doing wrong?
models.py
class Tenant(TenantMixin):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=100)
created_on = models.DateField(auto_now_add=True)
objects = TenantManager()
auto_create_schema = True
auto_drop_schema = True
class Domain(DomainMixin):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
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)
managers.py
class TenantManager(models.Manager):
def list_all(self):
return self.domains.all()
viewsets.py
class TenantViewSets(viewsets.GenericViewSet):
authentication_classes = (JWTAuthentication,)
permission_classes = [IsAuthenticated, IsAdminUser, IsSuperUser]
def list(self, request):
queryset = Tenant.objects.list_all()
serializer = Tenant.Serializer(queryset, many=True)
return Response(serializer.data)
serializers.py
class DomainSerializer(serializers.ModelSerializer):
tenant = serializers.RelatedField(read_only=True)
class Meta:
model = Domain
fields = (
"id",
"domain",
"tenant",
)
class TenantSerializer(serializers.ModelSerializer):
domain = DomainSerializer(read_only=True)
class Meta:
model = Tenant
fields = (
"id",
"name",
"schema_name",
"created_on",
"domain",
)
CodePudding user response:
You can't use the related_name
on a manager method like that. On the manager you can do all the usual ORM queries, like self.filter()
or self.get(id=1)
.
Looking at your code, if list_all()
is to return all domains but then you're trying to use that queryset in a view set for tennants. If it's doamin objects you actually want, change the queryset to Domain.objects.all()