从列表创建django表单时出错。

时间:2013-01-31 22:10:52

标签: python django

错误:

'list' object has no attribute '_meta'

forms.py:

from django.db import models
from django.forms import ModelForm
from django import forms


class Blog(models.Model):
    blog_id = models.CharField(max_length=100)

class TumblrForm(ModelForm):
    class Meta:
        model = Blog
        fields = ('blog_id',)
        widgets = {
            'blog_id':forms.RadioSelect(),
        }

表单创建:

form = TumblrForm(instance=blogs_id) #blogs_id is a list of blog urls

我哪里错了?

1 个答案:

答案 0 :(得分:1)

ModelForm编辑一个实例。您无法将其传递给网址列表。它需要一个Blog的实例。

TumblrForm(instance=Blog.objects.get(pk=1))

如果你想编辑多个ModelForms,你需要查看django的FormSet