You may have to override the UniqueTogetherValidator to use the correct
field.

https://www.django-rest-framework.org/api-guide/validators/#uniquetogethervalidator

~Carl


On Fri, Sep 11, 2020 at 3:25 PM Michael Goffioul <michael.goffi...@gmail.com>
wrote:

> I have the following model:
>
> class UnitData(models.Model):
>     unit = models.ForeignKey(Unit, on_delete=models.CASCADE)
>     key = models.CharField(max_length=64)
>     data = models.TextField(blank=True)
>
>     class Meta:
>         unique_together = ["unit", "key"]
>
> I would like to rename the field 'unit' to 'device' in my serializer, so
> I've used the following:
>
> class UnitDataBatchSerializer(serializers.HyperlinkedModelSerializer):
>     device = serializers.HyperlinkedRelatedField(
>             source='unit',
>             view_name='unit-detail',
>             queryset=Unit.objects.all())
>
>     class Meta:
>         model = UnitData
>         fields = [ 'device', 'key', 'data' ]
>
> However, I'm getting an exception when validating posted data. It appears
> the UniqueTogetherValidator will try to access the 'unit' field in the
> serializer, but it doesn't exist:
>
> https://github.com/encode/django-rest-framework/blob/master/rest_framework/validators.py#L109
>
> Is that expected? Or a bug? Is there a better way to rename the field in
> the serializer?
>
> Thanks,
> Michael.
>
> --
> 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-framework+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/049037ba-0104-404e-a921-570992b91d45n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-rest-framework/049037ba-0104-404e-a921-570992b91d45n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
-------------------------------------------------------------------------------
Carl J. Nobile (Software Engineer)
carl.nob...@gmail.com
-------------------------------------------------------------------------------

-- 
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-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CAGQqDQLgOVW3MwOsYd4JUTbjA-qv74YTmgasfkdOVTwQX53A4g%40mail.gmail.com.

Reply via email to