2013-07-15 2 views
0

exclude에 대한 공식 문서를 이해할 수 없습니다. 다음과 같이Django : 양식 저장 excelde 특정 필드

Set the exclude attribute of the ModelForm‘s inner Meta class to a list of fields to be excluded from the form. 

For example: 

class PartialAuthorForm(ModelForm): 
    class Meta: 
     model = Author 
     exclude = ['title'] 
Since the Author model has the 3 fields name, title and birth_date, this will result in the fields name and birth_date being present on the form. 


나의 이해는 다음과 같습니다 ('뭔가'), '뭔가'필드가 프론트 엔드에 = 표시되지 않습니다 제외되는 한 세트 data.If 모든 양식을 저장합니다 저장 방법 양식을 장고 양식 저장 방법을 호출하는 동안 저장되지 않습니다.
하지만 내가 말하는 문서로 할 때 'something'필드이 여전히 표시됩니다. 무슨 문제입니까?

저장을하지 않고 프론트 엔드에 표시 할 수있는 유효성 검사 양식에 일부 필드를 추가하고 싶습니다.이 필요성에 관해서는 아무 것도 발견하지 못했습니다.



**update** 

내 코드 :

class ProfileForm(Html5Mixin, forms.ModelForm): 

    password1 = forms.CharField(label=_("Password"), 
           widget=forms.PasswordInput(render_value=False)) 
    password2 = forms.CharField(label=_("Password (again)"), 
           widget=forms.PasswordInput(render_value=False)) 

    captcha_text = forms.CharField(label=_("captcha"), 
           widget=forms.TextInput()) 
    captcha_detext = forms.CharField(
           widget=forms.HiddenInput()) 

    class Meta: 
     model = User 
     fields = ("email", "username") 
     exclude = ['captcha_text'] 

    def __init__(self, *args, **kwargs): 
     super(ProfileForm, self).__init__(*args, **kwargs) 
     .......... 

    def clean_username(self): 
     ..... 

    def clean_password2(self): 
     .... 

    def save(self, *args, **kwargs): 
     """ 
     Create the new user. If no username is supplied (may be hidden 
     via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or 
     ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so 
     that if profile pages are enabled, we still have something to 
     use as the profile's slug. 
     """ 
     .............. 

    def get_profile_fields_form(self): 
     return ProfileFieldsForm 

경우을 제외에만 너무 exclude = ['captcha_text']이 작동하지 않을 것입니다, 클래스 메타에 정의 모델에 영향을 줍니까?

답변

0

exclude = ['title']은 모델이 아닌 양식에서 입력란을 제외합니다. form.save()은 사용할 수있는 필드가있는 모델 인스턴스를 저장하려고 시도하지만 모델은 누락 된 필드와 관련된 오류를 발생시킬 수 있습니다.

class PartialAuthorForm (ModelForm): 
    extra_field = forms.IntegerField() 

    class Meta: 
     model = Author 

    def save(self, *args, **kwargs): 
     # do something with self.cleaned_data['extra_field'] 
     super(PartialAuthorForm, self).save(*args, **kwargs) 

을하지만 모델 작성자에서 "PartialAuthorForm"라고 더 필드가 없는지 확인 :

이 작업을 수행, 모델 형태로 추가 필드를 추가합니다.

+0

따라서 save 메소드는 ** class Meta **에 정의 된'model'을 저장하는 데에만 사용되며 extra_field는 저장되지 않습니다. – Mithril

+0

extra_field가 모델 자체에 없으므로 어디에서 예상합니까? 저장하려면 extra_field를 사용 하시겠습니까? – Sudipta

+0

고마워요! 이제 알 겠어요. 대형 장고 프로젝트에서 약간의 수정을하는 것은 정말 어려운 방법입니다 ... – Mithril

0

먼저 제목 필드가 표시되는 이유는보기의 어딘가에 있어야합니다. 이처럼 (언 바운드) 형태의 인스턴스를 만들 수 있는지 확인 :

form = PartialAuthorForm() 

및 템플릿에

{{ form.as_p }} 

둘째 간단한 렌더링 방법을 시도, 여분의 필드를 추가 할 문제가 없습니다 모델 형식, 예를 참조하십시오. this 게시물.