Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:47:32+00:00 2026-06-04T02:47:32+00:00

I have the following models: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True, related_name=’profile’) starred_authors =

  • 0

I have the following models:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True, related_name='profile')
    starred_authors = models.ManyToManyField(Author, related_name='users_with_stars')

class Author(models.Model):
    name = models.CharField(max_length=250)
    slug = models.SlugField(unique=True)

And then in the author template, I would like to show whether or not the currently logged in user has starred that author, but I am running into trouble constructing a suitable query to get this data with the ORM.

In the view I had thought to annotate an author object with a count that will be 0 if the author has not been starred, or 1 if starred. Something like this:

def author_detail(request, slug):
    user_id = request.user.pk
    author = Author.objects.filter(slug=slug).filter(users_with_stars__user__pk=user_id).annotate(fav=Count('users_with_stars'))[0]
    return render_to_response('app/author_detail.html',
                              { 'author': author},
                              context_instance=RequestContext(request)) 

But the above is no good because no objects are found if the current user hasn’t starred the author. What I would like to do is somehow put the last filter inside the annotate, but I haven’t been able to get that working.

Ideally in the template I want to do something like:

{% if author.fav %}
    <p>This is one of your favorite authors</p>
{% endif %}

Any advice is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:47:34+00:00Added an answer on June 4, 2026 at 2:47 am

    I would probably split this out into a separate property in the template dictionary and calculate it by checking directly whether this user is in the list of users that starred this author.

    Something like:

    [...]
    author = Author.objects.filter(slug=slug)
    is_fav = author.users_with_stars.filter(pk=user_id).exists()
    return render_to_response('app/author_detail.html',
                              { 'author': author,
                                'is_fav': is_fav },
                              context_instance=RequestContext(request))
    

    And in the template:

    {% if is_fav %}
        <p>This is one of your favorite authors</p>
    {% endif %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
I have the the following Django model: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) full_name
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
Following is the models.py: class UserProfile(models.Model): user = models.OneToOneField(User) belongs_to_user_category = models.ForeignKey(UserCustomCategory, null=True, blank=True)
I have the following two models: class Provider(models.Model): provider = models.CharField(max_length=100, unique=True) class UserProfile(models.Model):
I have the following subclass: class UserProfile(User): user = models.OneToOneField(User, parent_link=True) And an UserProfileAdmin:
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) primary_phone = models.OneToOneField('Phone', related_name='is_primary',
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.