Django - model.save(commit=False - Is there a way to replicate this?

Posted by orokusaki on Stack Overflow See other posts from Stack Overflow or by orokusaki
Published on 2010-03-11T18:56:28Z Indexed on 2010/03/11 18:59 UTC
Read the original article Hit count: 568

Filed under:
|

I'm wanting to do this:

from django.contrib.auth.models import User

class PetFrog(models.Model):
    user = models.OnetoOneField(User)
    color = models.CharField(max_length=20)

    def clean(self):
        if self.color == 'Green':
            user = User(username='prince')
            user.save(commit=False)  # No commit argument in models.Model.save() like there is in ModelForm.save()
            user.set_password(self.password)
            user.save()
            self.user = user

Is there a way to do this creation of a model instance without filling in all the required fields, and then setting them manually before trying to save() for real (which would obviously raise a "Must choose a Password" error)? I need to do this in my model, vs using a ModelForm.

If there is another way to do it (while still in clean()), I'm completely open to any suggestions.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models