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:
Is that expected? Or a bug? Is there a better way to rename the field in the serializer?
Thanks,
Michael.