我有一个这样的课程:
class ControlVocabulary(models.Model):
definition = models.TextField()
term = models.CharField(primary_key=True, max_length=255)
class Meta:
abstract = True
为什么我不能使用'定义'在一个儿童班里面?有没有办法可以做到这一点?
class ActionType(ControlVocabulary):
definition = ControlVocabulary.definition # <-- Error
class Meta:
#...
更新: 看起来这在Django中是不允许的,但我仍然在寻找解决这个问题的方法。 In Django - Model Inheritance - Does it allow you to override a parent model's attribute?
我的观点类:
class VocabulariesView(ListView):
queryset = []
template_name = 'cvinterface/index.html'
def get_context_data(self, **kwargs):
context = super(VocabulariesView, self).get_context_data(**kwargs)
context['vocabulary_views'] = [{'name': vocabularies[vocabulary_name]['name'], 'definition': vocabularies[vocabulary_name]['definition'], 'url': reverse(vocabulary_name)}
for vocabulary_name in vocabularies]
return context
词汇表词典的一部分:
vocabularies = {
# optional keys:
# list_view, detail_view, list_template, detail_template
'actiontype': {
'name': ActionType._meta.verbose_name,
'definition': ActionType._meta.definition,
'model': ActionType,
'detail_template': 'cvinterface/vocabularies/actiontype_detail.html',
},
答案 0 :(得分:1)
您不必在definition
中定义ActionType
,因为它已经从ControlVocabulary
继承了
您可以按照以下方式检查:
x = ActionType.objects.all()
x[0].__dict__
其他检查方法是查看数据库中模型的字段
编辑:
尝试复制错误:
模型:
class ControlVocabulary(models.Model):
definition = models.TextField()
term = models.CharField(primary_key=True, max_length=255)
class Meta:
abstract = True
class ActionType(ControlVocabulary):
#definition = ControlVocabulary.definition # <-- Error
class Meta:
verbose_name='Action'
并在shell中:
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from testapp.models import *
>>> x = ActionType.objects.all()
>>> x
[]
>>> y = ActionType(definition='my definition')
>>> y.save()
>>> ActionType.objects.all()
[<ActionType: ActionType object>]