I am trying to accomplish the following with Django REST framework. I have model Records, which has field. It has foreign key to User. Each user can create multiple records with different numbers, but cannot create more than one record with the same number for themselves. However, each other user can create a record for itself with same number. E.g. Users Joe and Jill. Joe can create a record with number 123 only once, if he tries to make another record with 123, he should not be allowed. However Jill can create 123 once for herself, but not allowed more again.
My guess is to create a validator inside the serializer, which is
class RecordSerializer(serializers.HyperlinkedModelSerializer):
'''Serializer for Record'''
class Meta:
model = Record
fields = [
'user',
'number',
'otherfield',
]
validators = [
UniqueValidator(
queryset= Record.objects.filter(user=<current user>),
)
]
However, I cannot get the current user inside validator property, or how can I accomplish this otherwise?