Erez
Erez

Reputation: 1953

Having a wrird exception when adding ModelForm to create a form

Every thing is working great until i try to add a form with ModelForm. that throw the exception:

'str' object has no attribute '_default_manager'

My models and the ModelForm:

class Story(models.Model):
    title = models.CharField('סיפור', max_length=100, unique=True)
    body = models.TextField('תוכן') #RichTextField('תוכן')
    posted = models.DateField('תאריך פרסום', db_index=True)
    category = models.ForeignKey('Category', verbose_name='קטגוריה')
    created_by = models.ForeignKey(User, verbose_name='נכתב ע"י')

class Meta:
    verbose_name = 'סיפור'
    verbose_name_plural = 'סיפורים'

def __unicode__(self):
    return self.title


class StoryForm(ModelForm):    
    class Meta:
        model = Story 

class Category(models.Model):
title = models.CharField('קטגוריה', max_length=100, db_index=True)

class Meta:
    verbose_name = 'קטגוריה'
    verbose_name_plural = 'קטגוריות'

def __unicode__(self):
    return self.title

with the admin manager:

admin.site.register(Story)
admin.site.register(Category)

I had more stuff over there but i marked everything out and left only this lines to check, but still crashing...

I read that in previous django trunks it was a problem with the admin because of the admin manager, but i read that it was solved. I can't find the solution for why does this happen only when i try to add a ModelForm class.

Just don't understand, if it is because of the model or the admin, why didn't it happen until i am trying to add a form? if it is not, why a ModelForm can cause such a problem?

Just to be clear as possible, i marked every think out, all i have is the ModelForm definition, i am not calling it or anything.... it is in the creation of it that the exception happens.

Can any one please help me with this....I can't build a site without forms.... :-)

Thank you, Erez

Upvotes: 0

Views: 150

Answers (1)

Erez
Erez

Reputation: 1953

I've solved the problem....

Apparently, the problem what because the modelform was rendered before the category class so it didn't recognize it and tried to add it as a string....

moving the ModelForm class to forms.py solved the problem....

Upvotes: 1

Related Questions