M2M Field addition for logging purposes

10 views
Skip to first unread message

David

unread,
Mar 20, 2012, 5:40:56 AM3/20/12
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)

David

unread,
Mar 20, 2012, 1:10:12 PM3/20/12
to django...@googlegroups.com
In hindsight a m2m field was inappropriate and instead I have added a ForeignKey(Test) to the ModificationLog model.

Then added:

a = ModificationLog.objects.create(test=a, modifier=request.user)

after the successful save of any Test instances
Reply all
Reply to author
Forward
0 new messages