我正在使用Django 1.6
并使用模型继承。标题可能令人困惑,这是解释;
class ParentModel(models.Model)
class Meta:
db_table = "parent_model"
my_field=.....
class ProxyModelOfParentModel(ParentModel)
class Meta:
proxy=True
objects=CustomManager()
class InheritedModel(ParentModel)
class Meta:
db_table = "inherited_model"
my_extra_field=.....
假设这些是我们的模型。当我尝试在ParentModel上按 my_extra_field 进行过滤时,我会这样做;
ParentModel.objects.filter(inheritedmodel__my_extra_field='test')
但是,我想过滤代理模型,比如;
ProxyModelOfParentModel.objects.filter(inheritedmodel__my_extra_field='test')
当我运行它时,它无法在代理模型中找到字段 inheritedmodel 。这也可能是Django中的错误,我不知道。不知何故,当我尝试过滤代理模型时,django现在可以正确构建查询集。
为什么我使用代理模型而不是使用父模型,在不同的管理员中使用代理模型。当我在管理员list_filter
中提供密钥时,我遇到了FieldDoesNotExists
错误。
有没有办法过滤我提到的? 谢谢!
答案 0 :(得分:0)
正如我所提到的,这是因为代理模型初始化的django实现。我不知道,这是错误与否,但我需要以某种方式解决这个问题。我发现这是因为模型中的一部分_meta初始化。
django.db.models.options.py
中的这部分是我注释掉的部分导致了这个问题
def _fill_related_objects_cache(self):
cache = SortedDict()
parent_list = self.get_parent_list()
for parent in self.parents:
for obj, model in parent._meta.get_all_related_objects_with_model(include_hidden=True):
#THIS PART WAS CAUSING THE PROBLEM
# if (obj.field.creation_counter < 0 or obj.field.rel.parent_link) and obj.model not in parent_list:
# continue
if not model:
cache[obj] = parent
else:
cache[obj] = model
# Collect also objects which are in relation to some proxy child/parent of self.
proxy_cache = cache.copy()
for klass in get_models(include_auto_created=True, only_installed=False):
if not klass._meta.swapped:
for f in klass._meta.local_fields:
if f.rel and not isinstance(f.rel.to, six.string_types) and f.generate_reverse_relation:
if self == f.rel.to._meta:
cache[f.related] = None
proxy_cache[f.related] = None
elif self.concrete_model == f.rel.to._meta.concrete_model:
proxy_cache[f.related] = None
self._related_objects_cache = cache
self._related_objects_proxy_cache = proxy_cache
我只是覆盖我的父模型的Options类和元类,而不是覆盖Django本身就像;
class CustomProxyModelOptions(Options):
def _fill_related_objects_cache(self):
cache = SortedDict()
parent_list = self.get_parent_list()
for parent in self.parents:
for obj, model in parent._meta.get_all_related_objects_with_model(include_hidden=True):
if not model:
cache[obj] = parent
else:
cache[obj] = model
# Collect also objects which are in relation to some proxy child/parent of self.
proxy_cache = cache.copy()
for klass in get_models(include_auto_created=True, only_installed=False):
if not klass._meta.swapped:
for f in klass._meta.local_fields:
if f.rel and not isinstance(f.rel.to, six.string_types) and f.generate_reverse_relation:
if self == f.rel.to._meta:
cache[f.related] = None
proxy_cache[f.related] = None
elif self.concrete_model == f.rel.to._meta.concrete_model:
proxy_cache[f.related] = None
self._related_objects_cache = cache
self._related_objects_proxy_cache = proxy_cache
class ProxyModelMeta(ModelBase):
def __new__(cls, *args, **kwargs):
model = super(ProxyModelMeta, cls).__new__(cls, *args, **kwargs)
model._meta.__class__ = CustomProxyModelOptions
return model
class ParentModel(models.Model)
class Meta:
db_table = "parent_model"
my_field=.....
class ProxyModelOfParentModel(ParentModel)
__metaclass__= ProxyModelMeta
class Meta:
proxy=True
objects=CustomManager()
class InheritedModel(ParentModel)
class Meta:
db_table = "inherited_model"
my_extra_field=.....
现在,我可以过滤;
ProxyModelOfParentModel.objects.filter(inheritedmodel__my_extra_field='test')