Got it. That makes sense. Thanks! here's the traceback for the case where the field is excluded, so the validator is empty and so the integrity error exception never gets handled and the response is 500.
http://pastebin.com/UgvJBug2 DRF is v3.3.3, django is v1.8.5 On Thursday, March 23, 2017 at 11:29:27 PM UTC-7, Xavier Ordoquy wrote: > > > Le 24 mars 2017 à 02:00, Mark Mikofski <[email protected] <javascript:>> > a écrit : > > Hi Xavier, > > Thanks for your quick reply. I had to rush off and I didn't get to finish > the post. Sorry! > > Yes, I can make model objects and save them to the database using w = > Weather(latitude=x, longitude=y, header="blah", datatype=3) and w.save(). > > > There are slightly but significant differences between the .save() and > Weather.objects.create(..) > > The issue only occurs when I test the unique together constraint by > posting a duplicate. > > If the serializer looks like this: > > class WeatherSerializer(serializers.ModelSerializer): > """weather serializer""" > > class Meta: > model = Weather > exclude = ('lat_long',) > > > then I can post new records fine. But if I try to post a duplicate that's > when I get the integrity error, and since the validator is missing from the > serializer, the server says 500. > > > The traceback would help understand what we are speaking about. > > If I remove the line `exclude = ('lat_long')` then the serializer _does_ > have the `UniqueTogetherValidator` which I can see in a Django shell as > WeatherSerializer().validators > > However, now if I try to post a duplicate, instead of getting 500, I do > get 400, good, but the message is not "non_field_errors": ["The fields > lat_long, header, datatype must make a unique set."], instead I get > "lat_long": > ["This field is required."] > > * I realize I should/could use DecimalField for latitude and longitude and > that would resolve my problem. > * I tried using HiddenField but couldn't get it to make the lat_long value > without knowing the serializer object a priori > * SerializerMethodField with get_lat_long(obj) method didn't work either > > > If you have a unique constraint on those fields, serializer will blow > because it enforces the check before saving meaning the field isn’t ready. > Exclude the field, remove the constraint and perform the check by yourself. > > Regards, > Xavier Ordoquy, > Linovia. > > -- 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.
