Hi
I have a model such as:
class TestObj(models.Model):
owner = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
My serializer is like:
class TestObjSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
Model = TestObj
fields = ('name')
I have 2 issues.
Firstly, and most importantly, there must only be one TestObj per owner. Hence the OneToOneField. When I save currently, I get a postgres duplication violation error. I need to have my serializer return a validation error. How can I achieve this please? Should it be in my perform_create and perform_update methods? Or should it be in the serializer itself?
Secondly, if the validation takes place in the serializer, at no point do I want to expose the user in my API so therefor don't want to add it to fields = () Is that possible?
Many thanks