Opened 16 years ago
Closed 10 years ago
#9422 closed Bug (fixed)
Incorrect handling of foreign keys by serializers
Reported by: | Dave Hall | Owned by: | |
---|---|---|---|
Component: | Core (Serialization) | Version: | 1.0 |
Severity: | Normal | Keywords: | |
Cc: | david@…, Dave Hall | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
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.
Attachments (1)
Change History (11)
by , 16 years ago
comment:1 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
You forgot to include the patch to the test cases that fails beforehand and works afterwards. :-)
Also, I think this is more symptomatic of a larger problem that was sort of hacked around in r8957. Trying to fix that properly has shown it extends further than just that (particularly once you start chaining multiple foreign key references together, not just one level deep).
Definitely a bug here, but I'm not convinced that this is quite the right fix for it.
comment:2 by , 16 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
I've had quite a bit of trouble creating a test case (for tests/regressiontests/serializers_regress/*
) that emulates this situation. The code from r9601 and r9602 is a consistent way to try and fix it -- if everything that needs to do this uses the same method, we only have to fix bugs in one place, but it needs a test case.
Assigning back to the general pool for now, since it might be a little while until I get back to this, as I have other things to poke at. Writing a test case that fails with the existing code is something that anybody with a bit of persistence can do.
comment:3 by , 16 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
comment:4 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:5 by , 16 years ago
milestone: | 1.1 |
---|
comment:6 by , 14 years ago
Cc: | added |
---|---|
Severity: | → Normal |
Type: | → Uncategorized |
comment:7 by , 14 years ago
Type: | Uncategorized → Bug |
---|
comment:10 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
The models in the description now appear to serialize correctly in Django 1.7 (and maybe earlier versions).
Patch