Opened 8 years ago

Closed 22 months 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 8 years ago.
Patch

Download all attachments as: .zip

Change History (11)

Changed 8 years ago by Dave Hall

Attachment: patch.txt added

Patch

comment:1 Changed 8 years ago by Malcolm Tredinnick

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 Changed 8 years ago by Malcolm Tredinnick

Owner: Malcolm Tredinnick deleted
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 Changed 8 years ago by Malcolm Tredinnick

Needs tests: set
Patch needs improvement: set

comment:4 Changed 8 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:5 Changed 8 years ago by Russell Keith-Magee

milestone: 1.1

comment:6 Changed 6 years ago by Dave Hall

Cc: Dave Hall added
Severity: Normal
Type: Uncategorized

comment:7 Changed 6 years ago by Chris Beaven

Type: UncategorizedBug

comment:8 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:9 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:10 Changed 22 months ago by Tim Graham

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