Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Alexey Evseev: Trap in counting related objects in Django

$
0
0

Trap in counting related objects in Django

Task: for every object count number of related objects satisfying some conditions.

Example:

class Category(models.Model):
    title = models.CharField(max_length=50)

class Article(models.Model):
    title = models.CharField(max_length=50)
    category = models.ForeignKey(Category)
    approved_at = models.DateTimeField(blank=True, null=True)

Pay attention at field Article.approved_at, it contains article approval time and it can be null.

Create test data:

from django.utils import timezone

c1 = Category.objects.create(title='c1')
c2 = Category.objects ...

Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>