Opened 6 years ago

Last modified 6 years ago

#29472 closed Bug

Natural key not serializing for primary_key OneToOneField — at Initial Version

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

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.product3",
        "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 (0)

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