Home > Back-end >  Is partial form a good practice in Django?
Is partial form a good practice in Django?

Time:11-09

I'm new in Django and I try to find out if saving partial forms are a good practice or not. For example, I have Poll App and Candidate model with four fields: name, surname, targets andbiography. And I have a form where I have to fill all these fields. But if user only finished fill name, surname and targets, but even din't start filling biography field, how can I save his draft to user can finish it later and don't make any security mess?

models.py:

class Candidate(models.Model):
    name = models.CharField(max_length=50)
    surname = models.CharField(max_length=50)
    biography = models.TextField()
    targets = models.CharField(max_length=1000)

forms.py

class CandidateForm(ModelForm):
    class Meta:
        model = Candidate

        fields = [
            'name',
            'surname',
            'biography',
            'targets',
        ]

        widgets = {
            'biography': forms.Textarea(attrs={'rows': 3})
        }

I will be happy to see all ideas.

CodePudding user response:

how can I save his draft to user can finish it later and don't make any security mess?

You can either save the data to DB if you have designed the models to accept null or blank values, Or you can use Django sessions to temporarily store the form data until the form is completed...

In both case the security is not an issue since just like a DB access the django session data is also stored in a database and not in the browser... and so the end users cannot easily mess with the data...

check the following answer if you would like to know more...

CodePudding user response:

Your problem is here :-

models.py:

class Candidate(models.Model):
    name = models.CharField(max_length=50)
    surname = models.CharField(max_length=50)
    biography = models.TextField()
    targets = models.CharField(max_length=1000)

If you put default='your default value':-

models.py:

class Candidate(models.Model):
    name = models.CharField(max_length=50)
    surname = models.CharField(max_length=50)
    biography = models.TextField()
    targets = models.CharField(max_length=1000,default=' ')

I think this is right what you need

  • Related