Incorrect handling of foreign keys by serializers
|Reported by:||etianen||Owned by:|
|Cc:||david@…, etianen||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)
Changed 7 years ago by etianen
comment:1 Changed 7 years ago by mtredinnick
- Needs documentation unset
- Needs tests unset
- Owner changed from nobody to mtredinnick
- Patch needs improvement unset
- Status changed from new to assigned
comment:2 Changed 7 years ago by mtredinnick
- Owner mtredinnick deleted
- Status changed from assigned to new
comment:4 Changed 6 years ago by jacob
- milestone set to 1.1
- Triage Stage changed from Unreviewed to Accepted
comment:6 Changed 4 years ago by etianen
- Cc etianen added
- Severity set to Normal
- Type set to Uncategorized