class LatLongField(models.CharField):
"""
Custom field class based on :class:`~django.db.models.CharField` that
creates a string field from the site coordinates.
"""
def pre_save(self, model_instance, add):
"""
Create the lat long field the every time the model is saved.
"""
# save latitude and longitude with 3 decimal places
value = '%.3f %.3f' % (model_instance.latitude,
model_instance.longitude)
setattr(model_instance, self.attname, value)
return value
class Weather(models.Model):
latitude = models.FloatField()
longitude = models.FloatField()
header = models.CharField()
datatype= models.ChoiceField()
latlong = models.LatLongField()
--
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.
class WeatherSerializer(serializers.ModelSerializer):
"""weather serializer"""
class Meta:
model = Weather
exclude = ('lat_long',)
Le 24 mars 2017 à 02:00, Mark Mikofski <bwana...@gmail.com> 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().
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.
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().validatorsHowever, 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
class LatLongDefault(object):
"""
Provide default ``lat_long`` field for validation on POST.
"""
def set_context(self, serializer_field):
request = serializer_field.context['request']
LOGGER.debug('request data\n:%r', request.data)
self.latitude = float(request.data['latitude'])
self.longitude = float(request.data['longitude'])
def __call__(self):
return '%.3f %.3f' % (self.latitude, self.longitude)
def __repr__(self):
return unicode_to_repr('%s()' % self.__class__.__name__)
class WeatherSerializer(serializers.ModelSerializer):
"""weather serializer"""
lat_long = serializers.HiddenField(
default=serializers.CreateOnlyDefault(LatLongDefault())
)
class Meta:
model = Weather