开发者

django ModelForm for subclassed models : Error for one, but not for another ('NoneType' object has no attribute 'label')

开发者 https://www.devze.com 2023-01-11 07:36 出处:网络
I\'d like to ask for you guidance in the following matter in django: I am using the following models: class QItem(models.Model):

I'd like to ask for you guidance in the following matter in django:

I am using the following models:

class QItem(models.Model):
    isWhat = models.CharField(max_length=100, blank=True,  choices=ISWHAT)
    slug = models.SlugField(blank=True)
    script = models.CharField(max_length=100)
    comment = models.TextField(blank=True, null=True)
    author = models.ForeignKey(User)

class QuestionSet(QItem):
    items = models.ManyToManyField(QItem, blank=True, through='Ordering',related_name="contained")


class Question(QItem):
    answerObject = models.OneToOneField("AnswerObject", blank=True, null=True)

and their respective, most basic ModelForms:

class QuestionForm(ModelForm):
    class Meta:
        model = Question

class QuestionSetForm(ModelForm):
    class Meta:
       model = QuestionSet  

In a view, calling

           qset=QuestionFormSet()
           print q 

works just fine.

However,

           q = QuestionForm()
           print q

throws

Exception Type: AttributeError
Exception Value:    'NoneType' object has no attribute 'label'

in Django's server. When trying it in the console, I don't get any errors. Edit : Also, modelform_factory(Question) works.

Any ideas why this is the case? Why do similar models behave so differently, and how could I get rid of the error?

Edit : This is the full Traceback:

File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 100. response = callback(request, *callback_args, **callback_kwargs) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 25. return view_func(request, *args, **kwargs) File "/Library/WebServer/Documents/dj1/../dj1/esm/views.py" in dashboard 193. print qForm File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/utils/encoding.py" in str 27. return self.unicode().encode('utf-8') File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/forms/forms.py" in unicode 95. return self.as_table() File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/forms/forms.py" in as_table 217. errors_on_separate_row = False) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/forms/forms.py" in _html_output 145. bf = BoundField(self, field, name) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/forms/forms.py"开发者_开发问答 in init 398. if self.field.label is None:

Exception Type: AttributeError at /esm/dashboard/ Exception Value: 'NoneType' object has no attribute 'label'


Did you have something like fields = ("name") in the Meta of your Modelform ? The above statement is treated as an expression and not as a tuple. So the code is looking to validate field 'n','a','m','e' in the example above, as opposed to "name".

Please use a trailing comma to force python to interpret the Meta field above as a tuple fields = ("name",) ... your code should work fine after this change.

0

精彩评论

暂无评论...
验证码 换一张
取 消