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.
"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.
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.
--
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.