ModelFormSet to edit all entries for a model class
I have a simple model class:
class Category(BaseModel):
categoryText = db.StringProperty()
parentCat = db.IntegerProperty()
I want to create a model formset to display all entries of category:
form class:
class CategoryForm(forms.ModelForm):
categoryText = forms.CharField()
class Meta:
model = Category
View.py
def showAllCate开发者_JAVA技巧gories:
CategoryFormSet = modelformset_factory(Category)
if request.POST:
formset = CategoryFormSet(request.POST)
if formset.is_valid():
formset.save()
else:
formset = CategoryFormSet(Category.objects.all())
return render_to_response('showcat.html',{'formset':formset})
not sure what I'm doing wrong: I get this error: 'StringProperty' object has no attribute 'editable'
This should be really simple but it's been a challenge.
I realize this is a little bit old, but to fix your "'ModelFormOptions' object has no attribute 'many_to_many'" error, you might want to try changing the following code:
CategoryFormSet = modelformset_factory(CategoryForm)
to
CategoryFormSet = modelformset_factory(Category)
Use the name of the model instead.
Try this:
view.py
def showAllCategories(request):
CategoryFormSet = modelformset_factory(CategoryForm)
if request.method=='POST':
formset = CategoryFormset(request.POST)
if formset.is_valid():
...
formset.save()
...
else:
formset = CategoryFormset(queryset=Category.objects.all())
精彩评论