没有必填字段的Django表单 [英] Django Form with no required fields

查看:94
本文介绍了没有必填字段的Django表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个用于过滤搜索的表单,而不需要任何字段。例如给出这个代码:

I want to make a form used to filter searches without any field being required. For example given this code:

models.py:

models.py:

class Message(models.Model):
    happened = models.DateTimeField()
    filename = models.CharField(max_length=512, blank=True, null=True)
    message = models.TextField(blank=True, null=True)
    dest = models.CharField(max_length=512, blank=True, null=True)
    fromhost = models.ForeignKey(Hosts, related_name='to hosts', blank=True, null=True)
    TYPE_CHOICES = ( (u'Info', u'Info'), (u'Error', u'Error'), (u'File', u'File'), (u'BPS', u'BPS'),)
    type = models.CharField(max_length=7, choices=TYPE_CHOICES)
    job = models.ForeignKey(Jobs)

views.py:

WHEN_CHOICES = ( (u'', ''), (1, u'Today'), (2, u'Two days'), (3, u'Three Days'), (7, u'Week'),(31, u'Month'),)

class MessageSearch(ModelForm): #Class that makes a form from a model that can be customized by placing info above the class Meta
        message = forms.CharField(max_length=25, required=False)
        job = forms.CharField(max_length=25, required=False)
        happened = forms.CharField(max_length=14, widget=forms.Select(choices=WHEN_CHOICES), required=False)
        class Meta:
            model = Message

这是我现在的代码。您可以看到它基于模型形成一个表单。我重新定义了表单中的消息,因为我使用的是一个图标过滤器,所以我不需要一个巨大的文本框。我重新定义了日期,主要是因为我不想混淆日期(我讨厌工作日期!谁不?)而且我改变了工作领域,否则我得到了现有工作的下拉列表,我真的想通过常用词搜索。所以我可以将所有这些标记为不需要的。

That's the code I have now. As you can see it makes a form based on a model. I redefined message in the form because I'm using an icontains filter so I didn't need a giant text box. I redefined the date mostly because I didn't want to have to mess around with dates (I hate working with dates! Who doesnt?) And I changed the jobs field because otherwise I was getting a drop down list of existing jobs and I really wanted to be able to search by common words. So I was able to mark all of those as not required

问题是根据需要标记所有其他字段,因为在模型中,它们不允许为空白

The problem is it's marking all my other fields as required because in the model they're not allowed to be blank.

现在在模型中,它们不能为空。如果它们是空白的,那么数据是坏的,我不想在数据库中。但是,表单只是一个页面上的过滤器表单来显示数据。我永远不会从表格中保存,所以我不在乎字段是否空白。所以有一个简单的方法来使所有字段为必需= false同时仍然使用类Meta:model =消息格式的形式?这是非常方便的,我可以直接从一个模型的表单。

Now in the model they can't be blank. If they're blank then the data is bad and I don't want it in the DB. However the form is only a filter form on a page to display the data. I'm never going to save from that form so I don't care if fields are blank or not. So is there an easy way to make all fields as required=false while still using the class Meta: model = Message format in the form? It's really handy that I can make a form directly from a model.

这也是我第一次认真尝试在django应用程序,所以如果有什么荒谬的错误,请善意: )

Also this is my first serious attempt at a django app so if something is absurdly wrong please be kind :)

推荐答案

您可以创建一个适合您需要的自定义ModelForm。此自定义ModelForm将覆盖保存方法并将所有字段设置为不需要的:

You can create a custom ModelForm that suit your needs. This custom ModelForm will override the save method and set all fields to be non-required:

from django.forms import ModelForm

class SearchForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)
        for key, field in self.fields.iteritems():
            self.fields[key].required = False

所以你可以通过简单地调用FormForm来声明你的表单,例如:

So you could declare your forms by simply calling instead of the ModelForm, e.g.:

class MessageForm(SearchForm):
    class Meta:
        model = Message

这篇关于没有必填字段的Django表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆