0

I'm trying to do a Django annotate and want to list some of the object instances by filtering a field.

I have two model, which is Category and Article. This Category model has a field called super_category which is a choice field and the coices are "ACADEMIC, OTHER"

class Category(models.Model):
    name = models.CharField(
        max_length=128,
        null=False,
        blank=False,
        default=None,
        verbose_name=_("Name"),
    )

    super_category = models.CharField(
        blank=False,
        null=False,
        choices=SC_CHOICES,
        max_length=10,
        default=None,
    )

And now, this is my current annatation result:

[
   {
      'article_count': 716, 
      'super_category': u'ACADEMIC',
      'category_count': 5,
   }, 
   {
      'article_count': 800, 
      'super_category': u'OTHER',
      'category_count': 2,
   }
]

the query for this :

Category.objects.only(
   "id",
   "articles__id",
   "super_category",
).values(
   "super_category",
).annotate(
    category_count=Count("id", distinct=True),
    article_count=Count("articles"),
).order_by(
    "super_category",
)

What I wanna do is, adding the categories to the result

So at the end, I want something like this:

[
   {
      'article_count': 716, 
      'super_category': u'ACADEMIC',
      'category_count': 5,
      'categories': [ 
            {
                "id": 1,
                "name": "COMPUTER SCIENCE",
                "article_count": 15,
            },
            ...
      ]
   }, 
   {
      'article_count': 800, 
      'super_category': u'OTHER',
      'category_count': 2,
      'categories': [ 
            {
                "id": 1,
                "name": "MAGAZINE",
                "article_count": 15,
            },
            ...
      ]
   }
]

Now, since the additional "categories" is the same object type which I am trying to annotate, I seriously do not know how to do it.

Edit:

I am using a serializer as following:

class SuperCategorySerialiser(BaseSerializer):
    super_category = fields.CharField(read_only=True)
    article_count = fields.IntegerField(read_only=True)
    category_count = fields.IntegerField(read_only=True)

But note that I DO NOT have SuperCategory model.

Öykü
  • 252
  • 1
  • 3
  • 13
  • Please use a serializer. Even without annotations/... using `.values()` is *not* a good idea for serialization. – Willem Van Onsem Jul 24 '19 at 12:42
  • Thanks, I added to my question. I forgot to provide @WillemVanOnsem – Öykü Jul 24 '19 at 12:45
  • Is there by the way a reason why you do not use a `SuperCategory` model? – Willem Van Onsem Jul 24 '19 at 12:45
  • @WillemVanOnsem it was implemented like this beforehand, And the excuse for it "SuperCategory" can be 2 and nothing more. SuperCategory is just a string and do not have any info. A category can belong either Academic or Other and you can NOT create another "SuperCategory" later. – Öykü Jul 24 '19 at 12:57

1 Answers1

1

add this to your targeted serializer

 Cattegories = CategorySerializer(read_only=True,many=True)

then mention it in the fields of your targeted serializer

fields = ('X','Y','Categrories')

hope this helps :)

Aseel Ashraf
  • 681
  • 6
  • 12