dumpdata --natural-primary --natural-foreign not serializing pointers to parents in multi-table inheritance

161 views
Skip to first unread message

João Sampaio

unread,
Sep 10, 2016, 10:32:01 AM9/10/16
to django-d...@googlegroups.com
Hello!

I have these classes:

class SuperClassManager(models.Manager):
    def get_by_natural_key(self, identifier):
        return self.get(identifier=identifier)


class SuperClass(models.Model):
    objects = SuperClassManager()

    identifier = models.CharField(max_length=31, unique=True)

    def natural_key(self):
        return (self.identifier, )


class SubClass(SuperClass):
    pass


class SubClassTwo(SuperClass):
    pass

When I do

./manage.py dumpdata --natural-foreign --natural-primary --indent 4 app

I get:

[
{
    "model": "app.superclass",
    "fields": {
        "identifier": "one",
    }
},
{
    "model": "app.superclass",
    "fields": {
        "identifier": "two",
    }
},
{
    "model": "app.subclass",
    "fields": {}
},
{
    "model": "app.subclasstwo",
    "fields": {}
}
]

The problem is: superclass of identifier one is a subclass or a subclasstwo? There's no way to tell. Shouldn't the output be

[
{
    "model": "app.superclass",
    "fields": {
        "identifier": "one",
    }
},
{
    "model": "app.superclass",
    "fields": {
        "identifier": "two",
    }
},
{
    "model": "app.subclass",
    "fields": {
        "superclass_ptr": [
            "one"
        ]
    }
},
{
    "model": "app.subclasstwo",
    "fields": {
        "superclass_ptr": [
            "two"
        ]
    }
}
]

Now you can tell that superclass of identifier "two" is actually a subclasstwo!

Now, I did the homework. I digged into the code. :-)

Happens that OneToOneFields that are parent_links are marked as serialize=False, so they never get serialized! Even if you create the field manually in the model, if will still be marked as serialize=False.

Shouldn't OneToOneFields that point to parents in multi-table inheritance be serialized? Otherwise, how can you tell which subclass is the superclass in the serialized output?

What do you think? Do you think this is an issue to be resolved and, if so, can I submit a pull request?

Thanks.

charettes

unread,
Sep 10, 2016, 10:56:49 AM9/10/16
to Django developers (Contributions to Django itself)
Hi João,

It seems like this is an already reported bug[1] with an existing stale PR[2]
you might want to work on.

Cheers,
Simon

[1] https://code.djangoproject.com/ticket/24607
[2] https://github.com/django/django/pull/4473

João Sampaio

unread,
Sep 10, 2016, 11:04:28 AM9/10/16
to django-d...@googlegroups.com
Great! I've been wanting to contribute to Django for a long time, so I'll take a shot at this, hopefully I can help.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/5ef1c557-8364-488a-9462-764d4db4f6fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

João Sampaio

unread,
Sep 11, 2016, 8:48:19 AM9/11/16
to django-d...@googlegroups.com
Hello.

    I just sent a pull request for this bug, you can check it out here: https://github.com/django/django/pull/7231
    This is the first pull request I ever sent to a project, so I'm pretty excited. If there's something to fix in the pull request, let me know! There's a test to catch a regression and it's passing now.
    Also, is there any chance we can backport this to 1.10.2? I think this bug is critical because it causes loss of data if someone is exporting their database using dumpdata.

Thanks!
Reply all
Reply to author
Forward
0 new messages