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 [email protected] http://ryucoder.in <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> I’m protected online with Avast Free Antivirus. Get it here — it’s free forever. <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> On Thu, Mar 14, 2019 at 5:27 PM kumar deepak <[email protected]> wrote: > 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 > > > > > On Thu, Mar 14, 2019 at 4:32 PM Chetan Ganji <[email protected]> > wrote: > >> 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 >> [email protected] >> http://ryucoder.in >> >> >> >> <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> >> I’m >> protected online with Avast Free Antivirus. Get it here — it’s free >> forever. >> <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> >> <#m_-1312344716256754290_m_1721451009695702517_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> >> >> On Thu, Mar 14, 2019 at 4:17 PM kumar deepak <[email protected]> >> wrote: >> >>> 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. >>> >>> -- >>> 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 [email protected]. >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- >> 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 [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > -- > 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 [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
