0

I am working on a Django view which allows the user to modify an existing model object. However, when the form is submitted it creates a new object instead of modifying the existing object.

I have read the Django docs on the save() function, forms, queries, etc. but haven't been able to understand what is happening. The docs say that .save() should translate into an UPDATE statement on the database side.

I think the problem is related to the primary key for this model. I didn't define one manually. The form doesn't seem to be pulling in the ID field that Django automatically created. Since that ID didn't get passed along to .save(), it believes that I want a new object (because no pk was specified).

def modify(request, auditID):
    this_audit = get_object_or_404(Audit, id=auditID)

    form = auditForm(instance=this_audit)

    if form.is_valid():
        form.save()

    return redirect('index')

And the model:

from django.db import models


class Audit(models.Model):
    Inactive = '0'
    Completed = '1'
    InProgress = '2'
    Waiting = '3'

    status_choices = (
        (Inactive, "This audit is inactive. It may have been cancelled or    entered by mistake."),
        (Completed, "This audit is completed."),
        (InProgress, "This audit is currently in progress."),
        (Waiting, "This audit is waiting to be started."),
        )

    project_number = models.CharField(max_length=10, help_text="This is the audit project number. It should be in the format <b>A-xxx-xxx</b>.")
    title = models.CharField(max_length=300, help_text="This is the full audit title.")
    title_short = models.CharField(max_length=100, help_text="This is a short title that will be displayed when the long title is inconvenient.")
    status = models.CharField(max_length=1, choices = status_choices, default= Waiting, help_text="This is the current status of the audit.")

    def __str__(self):
        return self.title_short
indigochild
  • 350
  • 1
  • 5
  • 21
  • 4
    Post your model please. Do you have a custom primary key? Generally you don't need to set fields individually but use `form.save()` instead. – Selcuk Oct 11 '17 at 02:51
  • 1
    Why don't you actually validate data in the form? – Krzysztof Szularz Oct 11 '17 at 06:22
  • @Selcuk - I've added the model. Originally I did use form.save(), but replaced it as part of my troubleshooting. I thought perhaps I misunderstood how form.save() was working. – indigochild Oct 11 '17 at 14:27

1 Answers1

2

This would be better, is a better practice

def modify(request, auditID):
    this_audit = get_object_or_404(Audit, id=auditID)

    form = auditForm(request.POST, instance=this_audit)

    if form.has_changed():
        if form.is_valid():
            form.save()

    return redirect('index')

references https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#the-save-method . I'm not sure but basically the problem is that you have to use request.POST

Mauricio Cortazar
  • 4,049
  • 2
  • 17
  • 27