如何创建自定义窗口小部件,以便form.is_valid()返回true,而不是manytomany关系<select>输入?</select>

时间:2012-02-16 22:50:01

标签: python django django-forms widget

models.py:

class Tag(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=500, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now_add=True)

class Post(models.Model):
    user = models.ForeignKey(User)
    tag = models.ManyToManyField(Tag)
    title = models.CharField(max_length=100)
    content = models.TextField()
    created = models.DateTimeField(default=datetime.datetime.now)
    modified = models.DateTimeField(default=datetime.datetime.now)

    def __unicode__(self):
        return '%s,%s' % (self.title,self.content)


class PostModelForm(forms.ModelForm):
    class Meta:
        model = Post


class PostModelFormNormalUser(forms.ModelForm):
    class Meta:
        model = Post
        widgets = { 'tag' : TextInput() }
        exclude = ('user', 'created', 'modified')

    def __init__(self, *args, **kwargs):
        super(PostModelFormNormalUser, self).__init__(*args, **kwargs)      
        self.fields['tag'].help_text = None

views.py:

    if request.method == 'POST':
        form = PostModelFormNormalUser(request.POST)
        print form
        print form.errors           
        tagstring = form.data['tag']
        splitedtag = tagstring.split()

        if form.is_valid():
            temp = form.save(commit=False)
            temp.user_id = user.id
            temp.save()

            l = len(splitedtag)         
            for i in range(l):
                obj = Tag(name=splitedtag[i])
                obj.save()
                post.tag_set.add(obj)

            post = Post.objects.get(id=temp.id)
            return HttpResponseRedirect('/viewpost/' + str(post.id))
    else:
        form = PostModelFormNormalUser()
        context = {'form':form}
        return render_to_response('addpost.html', context, context_instance=RequestContext(request))

此处form.is_valid()始终为false,因为它会将标记从表单中获取为字符串。但它希望list为form.data ['tag']输入。任何人都可以告诉我如何解决它?

如何编写自定义小部件来解决此问题?

1 个答案:

答案 0 :(得分:1)

我认为您不需要自定义小部件(您仍然需要TextInput),您需要自定义 Field 。为此,您应该继承django.forms.Field。不幸的是,关于这个主题的文档很少:

  

如果内置的Field类不能满足您的需求,您可以轻松创建自定义Field类。为此,只需创建django.forms.Field的子类。它唯一的要求是它实现了一个clean()方法,并且它的init()方法接受上面提到的核心参数(required,label,initial,widget,help_text)。

我发现this blog post更深入地涵盖了自定义小部件和字段。作者不同意我上面引用的文档 - 值得一读。

根据您的具体情况,您可以执行以下操作(未经测试):

class MyTagField(forms.Field):
    default_error_messages = {
        'some_error': _(u'This is a message re: the somr_error!'),
    }

    def to_python(self, value):
        # put code here to coerce 'value' (raw data from your TextInput)
        # into the form your code will want (a list of Tag objects, perhaps)

    def validate(self, value):
        if <not valid for some reason>:
            raise ValidationError(self.error_messages['some_error'])

然后在你的ModelForm中:

class PostModelFormNormalUser(forms.ModelForm):
    tag = MyTagField()

    class Meta:
        model = Post
        exclude = ('user', 'created', 'modified')

    def __init__(self, *args, **kwargs):
        super(PostModelFormNormalUser, self).__init__(*args, **kwargs)      
        self.fields['tag'].help_text = None