Search code examples
djangodjango-modelsdjango-views

Django - concatenate all the groups for each post


I have 2 model in my Django app:

models.py:

class Post(models.Model):
    id = models.CharField(verbose_name="Post ID", max_length=1200, primary_key=True)
    post_name = models.CharField(
        verbose_name="Post Name",
        max_length=1000,
        blank=True,
        null=True,
    )
    post_description = models.TextField(
        verbose_name="Post Description",
        max_length=600,
        null=True,
        blank=True,
    )
    platform = models.CharField(
        verbose_name="Platform",
        max_length=25,
        null=True,
        blank=True,
    )
    type_dropdown = (
        ("Carousel ads", "Carousel ads"),
        ("Image ads", "Image ads"),
        ("Video ads", "Video ads"),
        ("Lead ads", "Lead ads"),
        ("Follower ads", "Follower ads"),
    )
    type = models.CharField(
        verbose_name="Type",
        max_length=15,
        blank=True,
        null=True,
        choices=type_dropdown,
        default="Type 1",
    )
    link = models.URLField(
        verbose_name="Link",
        max_length=4000,
        blank=True,
        null=True,
    )
    created_at = models.DateTimeField(default=now, null=True, blank=True)
    insert_date = models.DateTimeField(null=True, blank=True)
    user_last_update = models.ForeignKey(
        User,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
    )
    updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.post_name}"


class Group(models.Model):
    group_name = models.CharField(
        verbose_name="Group Name",
        max_length=150,
        blank=True,
        null=True,
    )
    post = models.ManyToManyField(
        Post,
        related_name="group_post",
    )
    created_at = models.DateTimeField(default=now, null=True, blank=True)
    insert_date = models.DateTimeField(null=True, blank=True)
    user_last_update = models.ForeignKey(
        User,
        related_name="combo_user",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
    )
    updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.group_name}"

I use in CBV (Django Class-Based Views) i i want to get a query set of all the posts with another column that concatenate all the groups for each post. How can i do that?

I tried this one but didn't work:

from django_mysql.models import GroupConcat

posts_with_groups = Post.objects.annotate(
    group_names=GroupConcat('groups__group_name')
)

i got this error: "Cannot resolve keyword 'groups' into field. Choices are: combo_post, created_at, id, insert_date..."


Solution

  • According to your help, I want to share the answer:

    views.py:

    from django.db.models import Value, CharField
    
    def get_queryset(self):
            posts_with_groups = Post.objects.annotate(
                groups=StringAgg(
                    F("group_post__group_name"), delimiter=", ", output_field=CharField()
                )
            )
            queryset = posts_with_groups
            print("My type:", type(queryset))
            return queryset