Last active
April 13, 2016 09:58
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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