How to disable unique validator for a django field with unique constraint.

4,119 views
Skip to first unread message

kumar deepak

unread,
Mar 14, 2019, 6:47:55 AM3/14/19
to Django REST framework
If I have a default serializer using a django model with one of the field being unique, data will always have is_valid() False. 
I may be using this data for update (not create), so making this invalid does not make sense.

Ideally if id (or primary key is provided), it should mean that the data will be used for update and unique validator should not be run.
Currently I can not use model->serialized_data->model save, is_valid fails in this case.

I must be missing something obvious. 

validators = [] also did not work, unique validation still happens. How can I override it or switch it off.

Chetan Ganji

unread,
Mar 14, 2019, 7:02:48 AM3/14/19
to django-res...@googlegroups.com
One way to solve this problem, is to have that model field not unique in the models. And enforce the uniqueness through code. 
So, you would write a validate_fieldname(self, value) method in the serializer to make sure that the field in question is unique and not as required
Hope this helps.


Regards,
Chetan Ganji
+91-900-483-4183


I’m protected online with Avast Free Antivirus. Get it here — it’s free forever.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kumar deepak

unread,
Mar 14, 2019, 7:57:10 AM3/14/19
to django-res...@googlegroups.com
This seems like removing an organ to eliminate pain. I have the following three problems for a nested model like this:

def Post(models.Model)
name = models.CharField(unique=True)
content = models.TextField()

def Comment(models.Model)
post = models.ForeignKey(Post)
content = models.CharField()

I created default model serializers with __all__ fields.

Problems:
1. The default model serializer does not work for nested models. I have to explicitly write create/update. This has been explained in the documentation, so nothing against it. Although I think choosing sane default can cater to 99% of use cases (and for the rest, behaviour can be customisable). I will try to take a shot at this.

2. When I try to use json from existing post object, serializer is_valid() fails saying "unique constraint on name fails". But I wanted it to update and not create. Should is_valid not be create/update aware based on id being passed in json.

3. When creating a new nested json with many comments, is_valid() fails saying that "post is empty". Of course I will not have post id in the json, as post creation is yet to happen. So is_valid becomes useless. Should is_valid not depend on if id is passed in json? Also, I can not use data/validated_data without having is_valid pass.

4. Setting validators = [] also does not remove field validations. I have not yet found a way to suppress field validations.

I have gone through source code and documentation and spent more than a day to set up something so simple.

I must be missing something simple, so any help is appreciated.

Thanks,
Kr Deepak



Chetan Ganji

unread,
Mar 14, 2019, 8:18:57 AM3/14/19
to django-res...@googlegroups.com
1. Right.

2. Dont know if you are using CBV (APIView) or FBV or ModelViewSets and the urls that you are hitting are PUT/POST/PATCH

3. Ofcourse, thats how foreign keys work. If a post is not created yet, who is going to comment on it?? Doesnt make sense. 
First create a post, get its id/pk, then update comments. Access fields inside the serializer using self.initial_data['fieldname'] before is_valid is called.

4. validators = [] will disable only code level checking, not database level checking.


Regards,
Chetan Ganji
+91-900-483-4183


I’m protected online with Avast Free Antivirus. Get it here — it’s free forever.

Reply all
Reply to author
Forward
0 new messages