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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With