In my ModelViewSet, I am trying to exclude my tenant field to prevent users from seeing the tenant id (from django-simple-multitenant). However, I am not able to develop a generic ModelSerializer because I am always required to define the model Meta in the ModelSerializer class. My source code that works (non-generic) is below. I would like to achieve either one of the options below:
Serializer works by picking up actual class model without need to specify the model in Meta class of the serializer
Set my Serializer in DEFAULT_MODEL_SERIALIZER_CLASS, and have it work (again without specifying the model in the Meta class of the serializer
It works well below, but when I assign the Serializer in DEFAULT_MODEL_SERIALIZER_CLASS, it does not seem to work.
class ExcludeTenantSerializer(serializers.ModelSerializer):
class Meta:
model = Product
exclude = ('tenant', )
class ProductViewSet(viewsets.ModelViewSet):
model = Product
ordering = ('id', )
serializer_class = ExcludeTenantSerializer
def get_queryset(self):
tenant = get_current_tenant()
if tenant:
return self.model.objects.filter(tenant=tenant)
else:
return self.model.objects.none()