change modelform values after POST

169 views
Skip to first unread message

Cesar Devera

unread,
Mar 21, 2010, 5:00:08 PM3/21/10
to Django users
hi.

I have a ModelForm based on an Model like this:

class MyModel(models.Model):
attr1 = models.CharField()
attr2 = models.CharField()
attr3 = models.CharField()
createdby = models.ForeignKey(User, related_name='createdby',
db_column='createdbyid')
calculatedfield = models.CharField()

class MyModelForm(ModelForm):
class Meta:
model = MyModel


the attribute fields attr1, attr2 and attr3 are properly shown on my
html page, and correctly restored on my server-side view like this:

def save(request,id):
user = request.user
try:
mymodel = MyModel.objects.get(pk=id)
except:
mymodel = MyModel()
form = MyModelForm(request.POST, instance=mymodel)

now comes the problem: I want to set the createdby attribute only on
my view, AFTER the html page Post. I want to set the createdby and any
other calculated field on server side. is it possible?

I tried:

form['createdby'] = request.user

but it didn't seem to work.

this field (createdby) is NOT NULL and so I wouldn't like to set it to
NULL, save the form with form.save() and later recover the entity
again and manipulate the remaining fields...

any ideas?

thanks in advance.

Bjunix

unread,
Mar 21, 2010, 5:38:39 PM3/21/10
to Django users
You probably want to change the attributes of the model object
directly.

"If you call save() with commit=False, then it will return an object
that hasn't yet been saved to the database."
Source: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

So I would do something like this:

def my_change_view(request, id):
#get instance based on id here
#....
if request.method == 'POST':
form = MyModelForm(request.POST, instance=instance)
if form.is_valid():
obj = form.save(commit=False)
obj.created_by = request.user
obj.save()
form.save_m2m()
return redirect('/somewhere')
else:
form = MyModelForm(instance=instance)
return render_to_response.....

I hope this helps. Remember to call form.save_m2m() if your model has
any many-to-many relations. I also would suggest to (re-)read django's
excellent documentation forms.

tc

unread,
Mar 27, 2010, 12:20:25 PM3/27/10
to Django users

On 21 mar, 22:38, Bjunix <bju...@bjunix.de> wrote:
> You probably want to change the attributes of the model object
> directly.
>
> "If you call save() with commit=False, then it will return an object
> that hasn't yet been saved to the database."

> Source:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-sav...


>
> So I would do something like this:
>
> def my_change_view(request, id):
>     #get instance based on id here
>     #....
>     if request.method == 'POST':
>         form = MyModelForm(request.POST, instance=instance)
>             if form.is_valid():
>                 obj = form.save(commit=False)
>                 obj.created_by = request.user
>                 obj.save()
>                 form.save_m2m()
>                 return redirect('/somewhere')
>     else:
>         form = MyModelForm(instance=instance)
>     return render_to_response.....
>
> I hope this helps. Remember to call form.save_m2m() if your model has
> any many-to-many relations. I also would suggest to (re-)read django's
> excellent documentation forms.
>

I see that the problem I have had today is exactly the same. The only
problem of the solution you are
proposing here is that created_by could not be mandatory. If it is,
the form would be not valid.

Paulo Almeida

unread,
Mar 29, 2010, 11:37:52 AM3/29/10
to django...@googlegroups.com
It depends on what you want. You can set 'created_by' to blank=True and null=False, so it doesn't have to be filled but it is still mandatory in the database. Or, if you never want to set it in the form, you can make it 'editable=False' and it doesn't show up in the form, but it can still be NOT NULL. Of course, you must make sure it is filled in some way.

- Paulo


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages