最初设置的默认Modelform字段值

时间:2013-08-02 14:20:31

标签: django default-value django-forms

我在初始加载表单时尝试为表单字段domainNm指定默认值。

我有: 的形式

   class SubmitDomain(ModelForm):
        class Meta:
            model = Tld #Create form based off Model for Tld
            fields = ['domainNm',]

    def clean_domainNm(self):
        cleanedDomainName = self.cleaned_data.get('domainNm')
        if Tld.objects.filter(domainNm=cleanedDomainName).exists():
            errorMsg = u"Sorry that domain is not available."
            raise ValidationError(errorMsg)
        else:
            return cleanedDomainName

    ## This creates the form.
    form = SubmitDomain()

模型

class Tld(models.Model):
    domainNm = models.CharField( )   #add a call in here?

我在发布之前尝试搜索谷歌很多,但似乎找不到一个如此基本的例子。

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

class SubmitDomain(ModelForm):
    domainNm = forms.CharField(initial=u'Initial value')
    class Meta:
        model = Tld #Create form based off Model for Tld
        fields = ['domainNm',]