Opened 8 years ago

Closed 15 months ago

#9422 closed Bug (fixed)

Incorrect handling of foreign keys by serializers

Reported by: etianen Owned by:
Component: Core (Serialization) Version: 1.0
Severity: Normal Keywords:
Cc: david@…, etianen Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no


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):

class Bar(Foo):
    name = models.CharField(max_length=244)
    slug = models.SlugField(unique=True)
    def __unicode__(self):

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 etianen 8 years ago.

Download all attachments as: .zip

Change History (11)

Changed 8 years ago by etianen


comment:1 Changed 8 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

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 7 years ago by mtredinnick

  • Owner mtredinnick deleted
  • Status changed from assigned to 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 Changed 7 years ago by mtredinnick

  • Needs tests set
  • Patch needs improvement set

comment:4 Changed 7 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 7 years ago by russellm

  • milestone 1.1 deleted

comment:6 Changed 5 years ago by etianen

  • Cc etianen added
  • Severity set to Normal
  • Type set to Uncategorized

comment:7 Changed 5 years ago by SmileyChris

  • Type changed from Uncategorized to Bug

comment:8 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:9 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:10 Changed 15 months ago by timgraham

  • Resolution set to fixed
  • Status changed from new to closed

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