David
unread,Mar 20, 2012, 5:40:56 AM3/20/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
Hello
I have a form for model test (code below) that saves correctly. I wish
to add records to the modificationlog model every time an instance of
test is saved. What should be saved is the logged in user and the time
of the modification. The below is what I have tried to achieve this
goal, but it does not create new modification log records, in fact at
the moment it doesn't even modify an existing related modificationlog
record.
Can someone point out where I am going wrong please?
Thank you for your time
class Test(models.Model):
first_name = models.CharField(max_length=255)
modification_log = models.ManyToManyField(ModificationLog)
class ModificationLog(models.Model):
modifier = models.ForeignKey(User)
modified_on = models.DateTimeField(auto_now=True)
@login_required
@transaction.commit_on_success
def test(request, pk):
a = get_object_or_404(Test.objects.select_related(), pk=pk)
if request.method == 'POST':
f = TestForm(request.POST, instance=a)
if f.is_valid():
modified =
ModificationLog.objects.create(modifier=request.user)
a.modificationlog.add(modified)
f.save()
else:
variables = RequestContext(request, {
'appointment': a,
'form': f,
})
return render_to_response('test/view_test.html',
variables)
else:
f = TestForm(instance=a)
variables = RequestContext(request, {
'test': a,
'form': f,
})
return render_to_response('test/view_test.html', variables)