Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - overriden form save() method returns None?

Currently, my model form is overriding save() method, and it works OK.The problem is when I call savedObject = form.save(), form get saved but it returns None? Before overriding save() I would get saved object, but what is the problem with this approach ?

save() in model form:

def save(self, *args, **kwargs):
       kwargs['commit']=False
       obj = super(MyForm, self).save(*args, **kwargs)
       obj.time = datetime.date.today()
       obj.expire = datetime.date.today()
       return obj.save()

call in my view after POST:

savedObject = form.save()
#form is now saved, but savedObject is None
like image 641
Zed Avatar asked Mar 03 '13 08:03

Zed


1 Answers

The correct way:

def save(self, commit=True):
    obj = super(MyForm, self).save(commit=False)
    obj.time = datetime.date.today()
    obj.expire = datetime.date.today()
    if commit:
        obj.save()
    return obj
like image 113
TigorC Avatar answered Sep 22 '22 13:09

TigorC