Reputation: 2214
I have a class like this:
class ControlVocabulary(models.Model):
definition = models.TextField()
term = models.CharField(primary_key=True, max_length=255)
class Meta:
abstract = True
Why can't I use 'definition' inside of a child class? Is there a way that I can do this?
class ActionType(ControlVocabulary):
definition = ControlVocabulary.definition # <-- Error
class Meta:
#...
UPDATE: It looks like this is not permitted in Django, but I am still searching for a work-around to this problem. In Django - Model Inheritance - Does it allow you to override a parent model's attribute?
My view class:
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
Part of vocabularies dictionary:
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',
},
Upvotes: 0
Views: 172
Reputation: 3068
You do not have to define the definition
in your ActionType
because it will be inherited already from your ControlVocabulary
You can check it already as follows:
x = ActionType.objects.all()
x[0].__dict__
Other way to check is by looking at the fields of you model in the database
EDIT:
Tried replicating the error:
models:
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'
and in the 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>]
Upvotes: 1