How to save a page as a draft

I created a detailed form in Django that has some required fields. I want to save the page as a variant of the project, without exception, if the required fields are not set at this time. How can i do this?

+3
source share
1 answer

The first set is required = False for fields that do not need to be set if you save as a draft. Then in your form add a field of type save_as_draft, which is logical. Now you need a method to check these fields depending on the state of the save_as_draft field (regardless of whether the user has selected this field or not). You can use this method:

def is_provided(self, field):
        value = self.cleaned_data.get(field, None)
        if  value == None or value == '':
            self._errors[field] = ErrorList([u'This field is required.'])
            if field in self.cleaned_data:
                del self.cleaned_data[field]

. :

def clean(self):
    draft = self.cleaned_data.get('save_as_draft', False)
    if not draft:
       # User doesn't save a draft so we need to check if required fields are provided
       req_fields = ['field1', 'field2', 'field3']
       for f in req_field:
           self.is_provided(f)

return self.cleaned_data

save_as_draft , - , save_as_draft . aa self.save_as_draft Form.clean, , .

,

+2

Source: https://habr.com/ru/post/1744912/


All Articles