cj ogbuehi
cj ogbuehi

Reputation: 187

Django model method wont update model field

I have a model object with a two methods: expired and pending. The expired manager works fine and updates the field. The pending manager does not work. Here is my code.

Side note: I set pending to true in a view.

models.py:

class Job(models.Model):
    expired=models.BooleanField(default=False)
    pending=models.BooleanField(default=False)
    purchased=models.DateTimeField(auto_now_add=True)
    time=models.PositiveIntegerField(blank=False)


    def job_expired(self):
        time=self.time
        date=self.purchased
        end=date+timedelta(days=time)
        #now is defined globally
        if now > end:
            ex=self.expired=True
            #i've tried these two ways below
            self.pending=False
            #or
            ax=self.pending=False
            ax.save()
        else:
            ex=self.expired=False
        return ex

Let me repeat that it works fine for updating the expired field but not the pending field. I also tried a separate method:

       def job pending(self):
            if self.expired:
                self.pending=False

None of these options seem to work. Can someone please help me out. Thanks

Upvotes: 1

Views: 1134

Answers (2)

catherine
catherine

Reputation: 22808

def job_expired(self):
    time=self.time
    date=self.purchased
    end=date+timedelta(days=time)
    #now is defined globally
    if now > end:
        self.expired = True
        self.pending = False 
        self.save()

        ex = self.expired
    else:
        ex=self.expired=False
    return ex

Upvotes: 2

David Wheaton
David Wheaton

Reputation: 618

In order to have the changes stick you need to update the model and then save it:

  1. self.pending = False
  2. self.save()

Also I think you mean field instead of manager. More info on model managers

Upvotes: 1

Related Questions