Skip to content

Instantly share code, notes, and snippets.

@aruseni
Last active April 13, 2016 09:58
Show Gist options
  • Save aruseni/741782d42e2095c38a4adfaf4992af66 to your computer and use it in GitHub Desktop.
Save aruseni/741782d42e2095c38a4adfaf4992af66 to your computer and use it in GitHub Desktop.
This Django model mixin adds an incr_count method that increments the “count” field in one SQL query (very useful if same object can be updated simultaneously and you want the count field to have correct values).
class CountIncrMethodMixin(models.Model):
def incr_count(self):
model = type(self)
model.objects.filter(id=self.id).update(
count=models.F("count")+1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment