Django指定模型的默认序列化器?

时间:2018-11-05 23:42:29

标签: python django

注意:这个问题是关于django.core.serialize,而不是django-field-history

现在,我正在使用一个名为“ django-field-history”的简洁小插件。我指定要跟踪的字段,并且只要其中一个字段发生更改,此插件就会创建日志。

对我来说,事情有点复杂,因为我要跟踪的模型是继承的模型

from polymorphic.models import PolymorphicModel
from accounts.models import Account

class Website(PolymorphicModel):

    account = models.OneToOneField(
        Account, blank=True, null=True,
        on_delete=models.SET_NULL, verbose_name=_('Account'))

    # Track account field with django-field-history
    field_history = FieldHistoryTracker([
        'account',
    ])

class WordpressWebsite(Website):
    wp_external_id = models.CharField(
        max_length=L, unique=True, validators=[validate_external_id])

因此django-field-history的工作原理是,它基本上调用django.core.serialize来序列化它正在跟踪的现有对象,并将序列化的数据存储到数据库中。序列化发生here

现在这成为一个问题,因为更改网站帐户时我看到的是null: enter image description here

我做了一些研究,发现“字段”是空的,因为模型是子类而不是根类。

问题:

如何为WordpressWebsite指定默认的序列化方法,以便调用serialize.serialize('json', wordpress_website, field_names=['account'])会返回类似[{"model": "websites.website", "pk": 242, "fields": {"account": 349}}]的内容 ?

0 个答案:

没有答案
相关问题