django_过滤器将%(应用程序标签)s_相关_名称作为字段名
(Django 1.8.14) 所以我有一个模型,它使用ContentType指向不同应用程序中的模型集合 我希望能够在单独的应用程序中“插入”将该模型链接到其他模型的功能。因此,我们有以下几点:django_过滤器将%(应用程序标签)s_相关_名称作为字段名,django,django-filter,django-filters,Django,Django Filter,Django Filters,(Django 1.8.14) 所以我有一个模型,它使用ContentType指向不同应用程序中的模型集合 我希望能够在单独的应用程序中“插入”将该模型链接到其他模型的功能。因此,我们有以下几点: class MyModelMixin(models.Model): """ Mixin to get the TheModel data into a MyModel """ updated_date = models.DateTimeField(null=True, blan
class MyModelMixin(models.Model):
""" Mixin to get the TheModel data into a MyModel
"""
updated_date = models.DateTimeField(null=True, blank=True)
submission = GenericRelation(
TheModel,
related_query_name='%(app_label)s_the_related_name')
class Meta:
abstract = True
主模型如下所示:
class TheModel(models.Model):
""" This tracks a specific submission.
"""
updated = models.DateTimeField()
status = models.CharField(max_length=32, blank=True, null=True)
final_score = models.DecimalField(
decimal_places=2, max_digits=30, default=-1,
)
config = models.ForeignKey(Config, blank=True, null=True)
content_type = models.ForeignKey(
ContentType,
blank=True,
null=True)
object_id = models.PositiveIntegerField(blank=True, null=True)
my_model_instance = GenericForeignKey()
mixin包含在MyModel模型中,可以在许多不同的应用程序中使用
使用django过滤器进行此操作时,存在一个问题。我有一个过滤器,应该在每个应用程序使用时对其进行实例化。但是,当我实例化时,例如
class MyFilterSet(Filterset):
def __init__(self, *args, **kwargs):
self.config_pk = kwargs.pop('config_pk', None)
if not self.config_pk:
return
self.config = models.Config.objects.get(pk=self.config_pk)
self.custom_ordering["c_name"] =\
"field_one__{}_the_related_name__name".format(
self.config.app_model.app_label,
)
super(MyFilterSet,self).__init__(*args, **kwargs)
然而,当我使用它时,我得到
FieldError: Cannot resolve keyword 'my_app_the_related_name field. Choices are: %(app_label)s_the_related_name, answers, config, config_id, content_type, content_type_id, final_score, form, form_id, id, object_id, section_scores, status, updated
(app_label)如何将_相关的_名称包含在字段集中,以及如何使其正确呈现(就像在django过滤器之外一样),或者是否有其他解决方案。您可能遇到过。在Django 1.9或更低版本上,在
GenericRelation
上模板化related\u query\u name
无法正常工作
在Django 1.10中添加的是,在合并之后 哦,很高兴知道。因为我已经深入到原始资料中了。我已经在原始资料中呆了很长时间了——非常感谢。