Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

435
Views
Django Model Not Updating After Calling .save()

My model won't update after save() is called in my view. Looking into several places (here, here) didn't yield any answers for my case. My model does contains a ManyToManyField:

Model:

class Event(models.Model):
    name = models.CharField(max_length=120)
    date = models.DateTimeField(auto_now=False, auto_now_add=False)
    attendees = models.ManyToManyField(Employee)
    approval = models.ForeignKey(EventStatus, default=2)

    def __unicode__(self):
        return self.name

    def approve_url(self):
        return reverse("RGAU:EventApprove", kwargs={"id": self.id})

View:

def EventApprove(request, id=None, **kwargs):

    instance = get_object_or_404(Event, id=id)

    instance.approval.id = 3
    instance.save()

    messages.success(request, "Event Approved")
    return redirect("RGAU:UserDashboard")

The goal is to have the Event instance updated to "Approved" when a button is pushed on the webpage (hence the view EventApprove).

instance.approval.id is suppose to assign the correct approval status id but the save is not committed.

What am I doing wrong?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can do this two different ways. If you have only the pk (id) value of the related object you want to associate, use the fieldname_id syntax, like this:

instance.approval_id = 3
instance.save()

If you have an instance of the relation handy, you can just assign it:

approved = EventStatus.objects.get(id=3)
instance.approval = approved
instance.save()

Either way, your instance.approval will now be associated with the EventStatus having the id=3.

To explain further: instance.approval is a property of instance which contains the associated instance of EventStatus. If you then further dig into that by using instance.approval.id you are now touching the EventStatus instance and changing its id, which you really don't want to do.

instance.approval_id is a little magic where Django allows you to directly set the database column value that associates the two models.. bypassing the need to actually fetch the instance of your EventStatus from the database just to be able to associate it with Event. It is handy in situations like this when you are using well known reference id values that never change. But even so it's good practice to define them somewhere, eg:

class EventStatus(models.Model):
    APPROVED = 3

    ... etc etc...

Then instead of using instance.approval_id = 3 you can use instance.approval_id = EventStatus.APPROVED which is a lot easier to understand later.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!