簡體   English   中英

保存來自Django的數據

[英]Saving data from a modelform Django

現在抬頭! 我是NOOBSVILLE的NOOB-BUS上的新菜鳥!

所以我正在處理一個表單,以加載信息並編輯該表單信息,這讓我很頭疼。 所以我正在使用:

Django:1.8 Pyhton:3.5.1后端是sqlite

我正在使用form.ModelForm將信息加載到其中,但是在保存時,這就是我遇到的問題。 該文檔非常令人困惑,如果我使用全部或僅一種清潔方式。

這是forms.py

    class EditContact(forms.ModelForm):
    class Meta:

    model = Contact
    #the list of all fields

    exclude = ['date_modified']


    def clean(self):
        if self.date_of_entry is None:
            print("looking to see what works")
            self.date_of_entry = datetime.date.today()
            return


    def clean_ContactID(self):
        #see this line below this comment i dunno what it does 
        ContactID= self.cleaned_data.get('ContactID')
        print ("cleaning it")
        # i also dont know what validation code suppose to look like
        # i cant find any working examples of how to clean data
        return ContactID

現在主要有更多的def clean_methods,但是我認為我要使用的是clean,應該使用所有,但在我看來。

這是在view.py中

def saveContactInfo (request):

    #this part i get 
    if  request.user.is_authenticated():

        ContactID= request.POST['ContactID']

        a = ListofContacts.objects.get(ContactID=ContactID)


        f = EditContact(request.POST,instance=a)       

        print("plz work!")
        if f.is_valid():
            f.save() 
            return render (request,"Contactmanager/editContact.html",   {'contactID': contactID})
        else:
            return HttpResponse("something isnt savin")

    else:
        return HttpResponse("Hello, you shouldnt ")

這是model.py

 def clean(self):

    if self.ConactID is None:
        raise  ValidationError(_('ContactID  cant be NULL!'))

    if self.date_of_entry is None:
        print("think it might call here first?")
        self.date_of_entry = datetime.date.today()
        print ( self.date_of_entry  )

    if self.modified_by is not None:
        self.modified_by="darnellefornow"
        print(self.modified_by )

    if self.entered_by  is not None:
        self.entered_by = "darnellefornow"
        print(self.entered_by )
        ContactID = self.cleaned_data.get('ContactID')

    return

現在,模型上方的字段和類型都具有空白= true和null = true,但排除的字段date_of_entry

我已經發現在視圖中調用is_valid()時會調用models.clean(),但是無法保存! 而且我不知道為什么! 我不知道如何進行驗證。 我想知道流程和所需的內容,甚至是表單驗證字段的示例。

我認為您在這里需要一些信息/答案,請查看您的代碼注釋。 希望這會有所幫助:

1)僅在需要處理專門針對該字段的自定義內容時,才需要使用clean_FIELDNAME函數。 Django文檔將其作為示例

def clean_recipients(self):
    data = self.cleaned_data['recipients']
    if "fred@example.com" not in data:
        raise forms.ValidationError("You have forgotten about Fred!")

    # Always return the cleaned data, whether you have changed it or
    # not.
    return data

因此,在該區域中,他們將檢查所提供的電子郵件列表是否包含特定的電子郵件。

2)這也顯示了您在評論中詢問的另一個問題,即如何處理驗證。 您會在上面的代碼段中看到,可以引發一個forms.ValidationError。 在此處進行更多討論: https : //docs.djangoproject.com/en/1.10/ref/forms/validation/

因此,如果在任何clean_方法或主clean方法中引發錯誤,則form.is_valid()將為false。

有幫助嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM