Home > Mobile >  Django - Return posts with avatar url of creator
Django - Return posts with avatar url of creator

Time:12-15

I have this class called User, which has an ImageField attribute, which stores the user's avatar/profile picture. I'm trying to annotate a Post query set so that just the user's avatar url gets returned with the post none of the other data. I've tried 2 different ways to annotate the query, but get errors as you can see below when I ping the views to get posts. How do I attach user avatar url with the return annotated Post query set?

models.py

class User(AbstractDatesModel):
    uuid = models.UUIDField(primary_key=True)
    username = models.CharField(max_length=USERNAME_MAX_LEN, unique=True, validators=[
        MinLengthValidator(USERNAME_MIN_LEN)])
    created = models.DateTimeField('Created at', auto_now_add=True)
    updated_at = models.DateTimeField('Last updated at', auto_now=True, blank=True, null=True)
    avatar = models.ImageField(upload_to=avatar_directory_path, blank=True, null=True)

    @property
    def avatar_url(self):
        return self.avatar.url

class Post(models.Model):
    uuid = models.UUIDField(primary_key=True, default=generate_ulid_as_uuid, editable=False)
    created = models.DateTimeField('Created at', auto_now_add=True)
    updated_at = models.DateTimeField('Last updated at', auto_now=True, blank=True, null=True)
    creator = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="post_creator")
    body = models.CharField(max_length=POST_MAX_LEN, validators=[MinLengthValidator(POST_MIN_LEN)])

annotator.py

def query_to_full_post_data_serializer(post_query_set: QuerySet):
    query_set_annotated = post_query_set.annotate(
        creator_username=F('creator__username'),
        creator_avatar_url=F('creator__avatar_url')
    )
    return FullPostDataSerializer(query_set_annotated, many=True)

annotator_version_2.py

def query_to_full_post_data_serializer(post_query_set: QuerySet):
    query_set_annotated = post_query_set.annotate(
        creator_username=F('creator__username'),
        creator_avatar_url=F('creator__avatar__url')
    )
    return FullPostDataSerializer(query_set_annotated, many=True)

annotator.py gives the error:
django.core.exceptions.FieldError: Cannot resolve keyword 'avatar_url' into field. Choices are: avatar, cheer_post, cheer_reply, created, creator_username, followee_id, follower_id, friendship_creator, friendshiprequest, goal_creator, goal_follow, goal_join, post_creator, reply, updated_at, userinterests, username, uuid

annotator_version_2.py gives the error:
django.core.exceptions.FieldError: Cannot resolve keyword 'url' into field. Join on 'avatar' not permitted.

CodePudding user response:

You should use this instead.

def query_to_full_post_data_serializer(post_query_set: QuerySet):
    query_set_annotated = post_query_set.annotate(
        creator_username=F('creator__username'),
        creator_avatar_url=F('creator__avatar')
    )
    return FullPostDataSerializer(query_set_annotated, many=True)
  • Related