Incorrect handling of foreign keys by serializers
|Reported by:||Dave Hall||Owned by:|
|Cc:||david@…, Dave Hall||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||yes|
When a foreign key points to a remote object via a non-primary key field, the Python serializer and XML serializer classes can sometimes handle this incorrectly.
This occurs when the primary key of the related object is a OneToOneField, as is the case for model inheritance. In this case, the serializer will store the unicode version of the primary key, rather than the primary key itself.
As an example:
class Foo(models.Model): pass class Bar(Foo): name = models.CharField(max_length=244) slug = models.SlugField(unique=True) def __unicode__(self): return self.name class Wibble(models.Model): bar = models.ForeignKey(Bar, to_field="slug")
when a Wibble instance is serialized, the value of the 'bar' property will be serialized as the result of calling unicode on the referenced Bar.
This problem can be resolved with the attached patch.
Change History (11)
comment:1 Changed 8 years ago by
|Owner:||changed from nobody to Malcolm Tredinnick|
|Patch needs improvement:||unset|
|Status:||new → assigned|