Django代理模型返回父模型

2 投票
1 回答
544 浏览
提问于 2025-04-18 08:53

我在使用django 1.6.5的时候遇到了一个奇怪的问题。

在distribution.models里,我有:

from core.models import Book, Person

class Proxy1(Book):
    class Meta:
        proxy = True

class Proxy2(Person):
    class Meta:
        proxy = True

这怎么会发生呢?

>>> from distribution.models import Proxy1, Proxy2
>>> type(Proxy1.objects.first())
<class 'core.models.Book'>
>>> type(Proxy2.objects.first())
<class 'distribution.models.Proxy2'>

有没有什么想法可以帮我找出问题的原因?

1 个回答

1

经过很多小时的寻找,我终于找到了问题的根源。django-money这个包里的MoneyField在模型管理器上做了一些复杂的操作,导致代理模型返回的模型类不正确。我已经在这里提交了一个问题:https://github.com/jakewins/django-money/issues/80

我找到了一种简单的解决办法,就是手动覆盖代理类中的'objects'属性,像这样:

class ProxyModel(SomeModelWithMoneyField):

    # This fixes django-money that would else return parent objects
    objects = models.Manager()

    class Meta:
        proxy=True

撰写回答