jeffci
jeffci

Reputation: 2622

How can I get form data into a model?

I have a model called Note:

class Note(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()


    def save(self, force_insert=False, force_update=False):
        from notes.tasks import build_htaccess
        super(Note, self).save(force_insert, force_update)
        build_htaccess.delay(self.id) # celery tasks

I have a modelform like so:

from django import forms

from notes.models import Note


class AddNoteForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)


    class Meta:
        model = Note

I do not want to store the password value in the database because I am overriding the save method to create an htaccess password via subprocess but I can't seem to access the form cleaned_data to get the password value in my model into the save override. Any suggestions as to how I can do this?

Upvotes: 0

Views: 134

Answers (1)

jonahs
jonahs

Reputation: 91

The question this raises for me is, if you're not storing the field value in the database why include it in a model at all?

I can't picture your precise endgame here, but what I would probably do in a situation similar to the one you're describing is create the password field by hand in the template. Then, I could grab the value of the field from the request.POST data within my view using something along the lines of the following:

if request.method == 'POST':
    [...]
    if myPassHandler(request.POST['password']):
        # password is valid
        [...]
    else:
        # bad password
        [...]

Where myPassHandler returns True or False. Hope this helps some.

Upvotes: 2

Related Questions