1

我试图在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?

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

任何帮助深表感谢。谢谢。

4

1 回答 1

1
class SubmitDomain(ModelForm):
    domainNm = forms.CharField(initial=u'Initial value')
    class Meta:
        model = Tld #Create form based off Model for Tld
        fields = ['domainNm',]
于 2013-08-02T14:26:15.757 回答