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)

patch.txt (1.5 KB ) - added by Dave Hall 16 years ago.
Patch

Download all attachments as: .zip

Change History (11)

by Dave Hall, 16 years ago

Attachment: patch.txt added

Patch

comment:1 by Malcolm Tredinnick, 16 years ago

Owner: changed from nobody to Malcolm Tredinnick
Status: newassigned

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 Malcolm Tredinnick, 16 years ago

Owner: Malcolm Tredinnick removed
Status: assignednew

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 Malcolm Tredinnick, 16 years ago

Needs tests: set
Patch needs improvement: set

comment:4 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:5 by Russell Keith-Magee, 16 years ago

milestone: 1.1

comment:6 by Dave Hall, 14 years ago

Cc: Dave Hall added
Severity: Normal
Type: Uncategorized

comment:7 by Chris Beaven, 14 years ago

Type: UncategorizedBug

comment:8 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:9 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:10 by Tim Graham, 10 years ago

Resolution: fixed
Status: newclosed

The models in the description now appear to serialize correctly in Django 1.7 (and maybe earlier versions).

Note: See TracTickets for help on using tickets.
Back to Top