Opened 7 years ago

Closed 7 years ago

#29472 closed Bug (duplicate)

Natural key not serializing for primary_key OneToOneField

Reported by: Ferd Snidely Owned by: nobody
Component: Core (Serialization) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ferd Snidely)

Serializing a model using a natural key that has a OneToOneField primary_key or parent_link fails to include the natural_key.

The problem occurs when serializing with use_natural_primary_keys=True,
and use_natural_foreign_keys=True. The parent_link field is not included
in the 'fields' structure in the json format (fails similarly for xml as
well).

Equivalent to this unanswered stackoverflow:
https://stackoverflow.com/questions/28702215/natural-key-serialize-django-model-with-one-to-one-field-as-primary-key

I have an example project at github/fsnidely/serialtest, which has an example model with tests.

An example of the expected serialized model with its natural_key ('thing'):

    {
        "model": "sandbox.product",
        "fields": {
            "thing": [
                "F-A"
            ],
            "prod_secs": 42,
            "low_level": -1
        }
    },

What is actually returned by serializers.serialize("json", ... or python manage.py dumpdata ...:

    {
        "model": "sandbox.product",
        "fields": {
            "prod_secs": 42,
            "low_level": -1
        }
    }

Notice that there is no PK or NK ('thing') for the object.

A simple parent model and child model inheriting parent model with a natural_key() is sufficient to reproduce the problem.

This problem appears to have existed in prior versions of Django as the referenced StackOverflow issue is over 3 years old.

Change History (2)

comment:1 by Ferd Snidely, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Resolution: duplicate
Status: newclosed

Seems to be a duplicate of #24607 as the tests in the sample project pass after the commit for that ticket.

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