title: How do I refresh the values on an object in Django?
body: <p>I have a model object in Django.  One of the methods on the object uses row-level locking to ensure values are accurate, like so:</p>
  
  <pre><code>class Foo(model.Model):
      counter = models.IntegerField()
  
      @transaction.commit_on_success
      def increment(self):
          x = Foo.objects.raw("SELECT * from fooapp_foo WHERE id = %s FOR UPDATE", [self.id])[0]
          x.counter += 1
          x.save()
  </code></pre>
  
  <p>The problem is if you call <code>increment</code> on a foo object, the object's values no longer reflect the values in the database.  I need a way to refresh the values in the object, or at least mark them as stale so they're refetched if necessary.  Apparently, this is functionality the developers of Django <a href="https://code.djangoproject.com/ticket/901">refuse to add</a>.</p>
  
  <p>I tried using the following code:</p>
  
  <pre><code>for field in self.__class__._meta.get_all_field_names():
      setattr(self, field, getattr(offer, field)) 
  </code></pre>
  
  <p>Unfortunately, I have a second model with the following definition:</p>
  
  <pre><code>class Bar(model.Model):
      foo = models.ForeignKey(Foo)
  </code></pre>
  
  <p>This causes an error, because it shows up in the field listing but you cannot <code>getattr</code> or <code>setattr</code> it.</p>
  
  <p>I have two questions:</p>
  
  <ul>
  <li><p><strong>How can I refresh the values on my object?</strong></p></li>
  <li><p><strong>Do I need to worry about refreshing any objects with references to my object, like foreign keys?</strong></p></li>
  </ul>