问题描述
我正在尝试构建两个称为SurveyQuestionBase
和SurveyResponseBase
的抽象类,它们将用作模板,以快速定义用于在我们的网站上实施特定调查的新具体模型.我遇到的问题是,强制将SurveyResponseBase
模型具体化后,应该为SurveyQuestionBase
的具体模型定义ForeignKey
.
I'm trying to build two abstract classes called SurveyQuestionBase
and SurveyResponseBase
that will serve as templates to quickly define new concrete Models for implementing specific surveys on our website. The issue I am having is in enforcing that the SurveyResponseBase
model, when made concrete, should define a ForeignKey
to a concrete model of SurveyQuestionBase
.
Django不允许我们定义ForeignKeys
来抽象类,因此,例如,我不能这样做:question = models.ForeignKey(SurveyQuestionBase)
出于类似原因,我也不能将其作为None
或app_label.ModelName
.
Django does not allow us to define ForeignKeys
to abstract classes so I cannot, for instance, do this:question = models.ForeignKey(SurveyQuestionBase)
Neither can I have it as None
or app_label.ModelName
for similar reasons.
一个有问题的修补程序是创建一个新的具体模型SurveyQuestionConcrete
,并指向ForeignKey
指向该位置:question = models.ForeignKey(concrete_model)
,并结合验证以确保替换了该模型.
One hacky fix is to create a new concrete model SurveyQuestionConcrete
and make the ForeignKey
point to this: question = models.ForeignKey(concrete_model)
, combined with validation to ensure this model is replaced.
是否有一种更清洁的方法来实现相同的目标? 我要做的就是确保当有人从SurveyResponseBase
定义具体模型时,他们包括从ForeignKey
到SurveyQuestionBase
Is there a cleaner way to achieve the same thing? All I need to do is ensure that when someone defines a concrete model from SurveyResponseBase
they include a ForeignKey
to a concrete model defined from SurveyQuestionBase
这是完整的代码:
from __future__ import unicode_literals
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
# Implementation borrows from: https://github.com/jessykate/django-survey/
class SurveyQuestionBase(models.Model):
TEXT = 'text'
INTEGER = 'integer'
RADIO = 'radio'
SELECT = 'select'
MULTI_SELECT = 'multi-select'
ANSWER_TYPE_CHOICES = (
(INTEGER, 'Integer',),
(TEXT, 'Text',),
(RADIO, 'Radio',),
(SELECT, 'Select',),
(MULTI_SELECT, 'Multi-Select',),
)
question = models.TextField()
required = models.BooleanField()
question_type = models.CharField(choices=ANSWER_TYPE_CHOICES, max_length=20)
class Meta:
abstract = True
class SurveyResponseBase(models.Model):
"""
concrete_question_model: 'app_label.Model' - Define the concrete model this question belongs to
"""
concrete_model = 'SurveyQuestionBase'
question = models.ForeignKey(concrete_model)
response = models.TextField()
class Meta:
abstract = True
推荐答案
针对此问题的两种解决方案(均有效):
Two solutions (both working) to this problem:
第一个解决方案涉及使用GenericForeignKey
.第二个更有趣,涉及动态生成SurveyResponseBase
.
The first solution involves using GenericForeignKey
. The second is more interesting and involves generating the SurveyResponseBase
dynamically.
class SurveyQuestionBase(models.Model):
TEXT = 'text'
INTEGER = 'integer'
RADIO = 'radio'
SELECT = 'select'
MULTI_SELECT = 'multi-select'
ANSWER_TYPE_CHOICES = (
(INTEGER, 'Integer',),
(TEXT, 'Text',),
(RADIO, 'Radio',),
(SELECT, 'Select',),
(MULTI_SELECT, 'Multi-Select',),
)
question = models.TextField()
required = models.BooleanField()
question_type = models.CharField(choices=ANSWER_TYPE_CHOICES, max_length=20)
class Meta:
abstract = True
@classmethod
def get_subclasses(cls, *args, **kwargs):
for app_config in apps.get_app_configs():
for app_model in app_config.get_models():
model_classes = [c.__name__ for c in inspect.getmro(app_model)]
if cls.__name__ in model_classes:
yield app_model
class SurveyResponseBase(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, limit_choices_to=get_content_choices)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
response = models.TextField()
class Meta:
abstract = True
def get_content_choices():
query_filter = None
for cls in SurveyQuestionBase.get_subclasses():
app_label, model = cls._meta.label_lower.split('.')
current_filter = models.Q(app_label=app_label, model=model)
if query_filter is None:
query_filter = current_filter
else:
query_filter |= current_filter
return query_filter
class SurveyQuestionBase(models.Model):
TEXT = 'text'
INTEGER = 'integer'
RADIO = 'radio'
RATING = 'rating'
SELECT = 'select'
MULTI_SELECT = 'multi-select'
QUESTION_TYPES = (
(INTEGER, 'Integer'),
(TEXT, 'Text'),
(RADIO, 'Radio'),
(RATING, 'Rating'),
(SELECT, 'Select'),
(MULTI_SELECT, 'Multi-Select'),
)
CHOICE_TYPES = (RADIO, RATING, SELECT, MULTI_SELECT)
question = models.TextField()
required = models.BooleanField()
question_type = models.CharField(choices=QUESTION_TYPES, max_length=20)
choices = models.TextField(blank=True, null=True)
choices.help_text = """
If the question type is "Radio," "Select," or "Multi-Select",
provide a comma-separated list of options for this question
"""
class Meta:
abstract = True
Meta = type('Meta', (object,), {'abstract': True})
def get_response_base_class(concrete_question_model):
"""
Builder method that returns the SurveyResponseBase base class
Args:
concrete_question_model: Concrete Model for SurveyQuestionBase
Returns: SurveyResponseBase Class
"""
try:
assert SurveyQuestionBase in concrete_question_model.__bases__
except AssertionError:
raise ValidationError('{} is not a subclass of SurveyQuestionBase'.format(concrete_question_model))
attrs = {
'question': models.ForeignKey(concrete_question_model, related_name='responses'),
'response': models.TextField(),
'__module__': 'survey_builder.models',
'Meta': Meta(),
}
return type('SurveyResponseBase', (models.Model,), attrs)
由于GenericForeignKeys方法需要额外的ContentType选择,因此我们决定继续解决方案2.
We decided to go ahead with Solution 2 since the GenericForeignKeys approach requires an additional ContentType selection.
这篇关于用ForeignKey定义另一个抽象模型的抽象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!