0

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',
},
Community
  • 1
  • 1
denvaar
  • 2,174
  • 2
  • 22
  • 26
  • worksforme (tm). https://gist.github.com/anonymous/d67daa4aa8c8966eb92a – iced Mar 10 '15 at 21:15
  • 2
    Could you post a more extensive error? It probably comes from Django itself, not python. – spalac24 Mar 10 '15 at 21:19
  • Why do you want to do this? `definition` will already be equal to `ControlVocabulary.definition` by default from inheritance. – vishen Mar 10 '15 at 22:11
  • @vishen With or without that line I still get an error message saying that definition is not defined. In regular Python code, this works fine, but with Django, this is not permitted (see edit.) I need to be able to access definition from my template, but I cannot figure out how to make it available. – denvaar Mar 10 '15 at 22:14
  • @DenverSmith So if you remove the `definition` from `ActionType` and `print ActionType.definition`, that fails? – vishen Mar 10 '15 at 22:18
  • @vishen yes, it fails with: NameError: name 'definition' is not defined – denvaar Mar 10 '15 at 22:21
  • 1
    I have a feeling you did not migrate your models after changing you structure – Abhishek Mar 10 '15 at 22:29

1 Answers1

1

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>]
Abhishek
  • 2,998
  • 9
  • 42
  • 93