There are some issues with the code. First, entry.id = entry_id is redundant. It might even be the cause of the new Entry. https://docs.djangoproject.com/en/dev/ref/models/instances/#how-django-knows-to-update-vs-insert Second, the best way to update from a form and code at the same time is to do save(commit=False). Updating your code: form = EntryForm(data=request.POST, instance=entry) if form.is_valid(): updated_entry = form.save(commit=False) updated_entry.owner = request.user updated_entry.save() return redirect('entry')
On Mar 10, 2012, at 7:48 PM, Zeengr wrote: > this is my code and weirdly it creates new entry instead of updating the > passed one in instance argument, don't know if that because of m2m > relationship in Entry model or my code is broken! > what could be the cause? > > ### my edit view > @login_required > def edit_entry_view(request, entry_id): > > try: > entry = Entry.objects.get(pk=entry_id, is_visible=True) > except ObjectDoesNotExist: > return render(request, 'entry/entry.html', {'no_enties': True}) > > if request.method == 'POST': > entry.owner = request.user > entry.id = entry_id > form = EntryForm(data=request.POST, instance=entry) > if form.is_valid(): > form.save() > return redirect('entry') > else: > form = EntryForm(instance=entry) > return render(request, 'entry/edit_entry.html', {'form': form}) > > ###my form > class EntryForm(ModelForm): > > class Meta: > model = Entry > exclude = ('owner') > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/MQ8mh7qDPHYJ. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. Peter of the Norse rahmc...@radio1190.org -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.