2016-07-27 17 views
0

私のGroupFormフォームでウィジェットを追加するとどうして私の検証が間違いになるのかわかりません。その前に、彼らは私のモデルを尊重していましたが、今はモデルを尊重することなくすべてのウィジェットattrsを追加した後、フィールドはすべてに必要です。ウィジェットを定義するときに私が見逃した他のアイテムがありますか?djangoのCreateViewとUpdateViewの検証は、モデルが存在しない場合でも常に起動されます

forms.py:

class GroupForm(forms.ModelForm): 
    group_name = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'1', 'placeholder':'Groups name'})) 
    group_contact = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'2', 'placeholder':'Groups point of contact person'})) 
    tin = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'3', 'placeholder':'Groups tin#'})) 
    npi = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'4', 'placeholder':'Groups npi#'})) 
    notes = forms.CharField(widget = forms.Textarea(attrs={'tabindex':'5', 'placeholder':'Group notes'})) 
    #notes = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'5', 'placeholder':'Groups notes'})) 

    billing_address = forms.ModelChoiceField(queryset=Address.objects.all(), widget=forms.Select(attrs={'tabindex':'6'})) 
    mailing_address = forms.ModelChoiceField(queryset=Address.objects.all(), widget=forms.Select(attrs={'tabindex':'7'})) 
    start_date = forms.DateField(widget=forms.TextInput(attrs= 
           { 
            'class':'datepicker', 
            'tabindex' : '8', 
            'placeholder' : 'Groups start date' 
           })) 
    end_date = forms.DateField(widget=forms.TextInput(attrs= 
           { 
            'class':'datepicker', 
            'tabindex' : '9', 
            'placeholder' : 'Groups term date' 
           })) 
    change_date = forms.DateField(widget=forms.TextInput(attrs= 
           { 
            'class':'datepicker', 
            'tabindex' : '10', 
            'placeholder' : 'Groups changed date' 
           })) 

    change_text = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'11', 'placeholder':'Reason for date change'})) 
    #term_comment = forms.CharField(widget= forms.TextInput(attrs={'tabindex':'12', 'placeholder':'Note on group term'})) 
    term_comment = forms.CharField(widget = forms.Textarea(attrs={'tabindex':'12', 'placeholder':'Note on group term'})) 
    group_phone = forms.RegexField(regex=r'^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$', 
           error_message = ("Phone number must be entered in the format: '555-555-5555 or 5555555555'. Up to 15 digits allowed."), 
           widget = forms.TextInput(attrs={'tabindex':'13', 'placeholder': '555-555-5555 or 5555555555'})) 

    group_fax = forms.RegexField(regex=r'^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$', 
           error_message = ("Fax number must be entered in the format: '555-555-5555 or 5555555555'. Up to 15 digits allowed."), 
           widget = forms.TextInput(attrs={'tabindex':'15', 'placeholder': '555-555-5555 or 5555555555'})) 

    group_term = forms.ModelChoiceField(queryset=GroupTerm.objects.all(), widget=forms.Select(attrs={'tabindex':'16'})) 

    class Meta: 
     model=Group 
     exclude = ['created_at', 'updated_at'] 

views.py:

class GroupCreateView(CreateView): 
    model = Group 
    form_class = GroupForm 
    template_name = 'ipaswdb/group/group_form.html' 
    success_url = 'ipaswdb/group/' 

    def form_valid(self, form): 
     return super(GroupCreateView, self).form_valid(form) 

class GroupUpdateView(UpdateView): 
    model = Group 
    form_class = GroupForm 
    template_name = 'ipaswdb/group/group_form.html' 
    success_url = 'ipaswdb/group/' 

グループモデル:

class Group(models.Model): 
    group_name = models.CharField(max_length=50) 
    group_contact= models.CharField(max_length=50) 
    tin = models.CharField(max_length=50) 
    npi =models.CharField(max_length=50) 
    notes = models.TextField(max_length = 255, null=True, blank=True) 
    billing_address = models.ForeignKey('Address', related_name = 'billing_address', on_delete=models.SET_NULL, null=True) 
    mailing_address = models.ForeignKey('Address', related_name = 'mailing_address', on_delete=models.SET_NULL, null=True, blank=True) 
    start_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True) 
    end_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True) 
    change_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True) 
    change_text = models.TextField(max_length = 255, null=True, blank=True) 
    term_comment = models.TextField(max_length = 255, null=True, blank=True) 
    group_phone=models.CharField(max_length=50) 
    group_fax = models.CharField(max_length=50) 
    group_term = models.ForeignKey(GroupTerm, on_delete=models.SET_NULL, null=True, blank=True) #quesiton is can a group be termed many times? 
    created_at=models.DateField(auto_now_add=True) 
    updated_at=models.DateField(auto_now=True) 

    #provider_location = models.ManyToManyField('ProviderLocations', through='GroupLocations') 

    def __str__(self): 
     return self.group_name 

答えて

1

あなたがウィジェットを追加しましたので、それはあなたが実際に再定義するので、それはだ、ではありませんそれらを再定義する際には、モデルの要件を尊重していませんでした。たとえば、ご使用のモデルで

mailing_address = models.ForeignKey(..., null=True, blank=True) 

郵送先住所は空白にすることができますが、定義されたフォームフィールドには必須です。

mailing_address = forms.ModelChoiceField(queryset=Address.objects.all(), widget=forms.Select(attrs={'tabindex':'7'})) 
# You need required=False 

あなたができるmodelFormのために独自のフィールドを再定義したい場合は、それをやっている間、あなたのモデルを尊重する必要があります。 でも、既に存在する辞書を使用して試してみることができます。modelFormあなたの内側たとえばclass Metaあなたはこのようにウィジェットを上書きすることができます。

class YourForm(ModelForm): 
    class Meta: 
     model = YourModel 
     fields = ('field_1', 'field_2', 'field_3', ...) 
     widgets = { 
      # CHANGE THE WIDGETS HERE IF YOU WANT TO 
      'field_1': Textarea(attrs={'cols': 80, 'rows': 20}), 
     } 
     labels ={ 
      # CHANGE THE LABELS HERE IF YOU WANT TO 
     } 
Djangoの modelFormドキュメントで

さらに詳しい情報:私はフィールドここで追加する方法がわからないですけれども、これは、かなりうまく機能しているOverriding defaults field

+0

感謝group_phone = forms.RegexField(regex = r '^(\ + \ d {1,2} \ s)?\(?\ d {3} \)?[\ s.- ] \ d {3} [\ s .-]?\ d {4} $ '、 error_message =( "電話番号は555-555-5555または5555555555の形式で入力する必要があります。 )、 widget = forms.TextInput(attrs = {'tabindex': '13'、 'プレースホルダ': '555-555-5555 or 5555555555'})) – Codejoy

+0

これらのフィールドを明示的に定義することができます。検証はモデルを尊重してください。あなたは 'RegexField'を使用するか、バリデーターを使って' CharField'を使うことができます。 [上書きデフォルトセクション](https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/#overriding-the-default-fields)の一番下にバリデータの例がありますが、これは必須ではありませんが、あなたはそれらを上書きすることができます。 –

+0

それはそれをしました、これは素晴らしいあなたに感謝して働いています! – Codejoy

関連する問題