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 6028469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:44:40+00:00 2026-05-23T04:44:40+00:00

I have two models that are related to each other. For a contrived example,

  • 0

I have two models that are related to each other. For a contrived example, the first is a list of things and the second is a list of people who have liked each thing.

I want to show a list of things and if I’ve liked one of them then show an icon next to it.

# in the models
class Things(models.Model):
    name = models.CharField(max_length = 100)

class Likes(models.Model):
    user = models.ForeignKey(User)
    thing = models.ForeignKey(Thing)

# in the view
@login_required
def list_of_things(request):
    things = things.objects.all()
    context = RequestContext(request, {'things': things})
    return render_to_response('thinglist.html', context)

# in the template
{% for thing in things %}
<li>{{ thing.name }}
  ## PSUEDO CODE HERE
  {% if I've liked this thing %}
  <img src="like.png">
  {% endif %}
</li>

I’ve found that in the python shell I can do this:

>>> thing.likes_set.filter(user=user)

and get what I want, but I’m not sure where in the code above I should put this. I thought for a bit that if I added a method to my Things model I could, in my template, do:

{% if thing.liked_by_me %}

But that would require the model know the username. Also it seems like it wouldn’t be the best performance.

Any suggestions?

  • 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-05-23T04:44:41+00:00Added an answer on May 23, 2026 at 4:44 am

    In your view for best performance you can get a list of things and list of things you like.

    def list_of_things(request):
        things = things.objects.all()
        things_i_like = Like.objects.filter(user=current_user).values_list('things', flat=True)
        context = RequestContext(request, {'things': things, 'things_i_like':things_i_like})
        return render_to_response('thinglist.html', context)
    

    values_list will will only select only ‘things’
    flat will flatten the QuerySet into a list

    {% for thing in things %}
    <li>{{ thing.name }}
      ## PSUEDO CODE HERE
      {% if thing in things_i_like %}
       ## IMAGE LINK
      {% endif %}
    </li>
    {% endfor %}
    

    Then the template can iterate through ‘things’ and check if a single thing is in the ‘things_i_like’ list

    I haven’t tested this but it should work…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I have two models- Users and Accounts. Each account can have at
I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that
I have two models: Person and Relation. The second one stores information about relations
Let's say I have two models, Classes and People. A Class might have one
I have two models that has a manytomany relationship with a 'through' table in
If I have two Models that have a manytomany relationship with a through model,
I have two different models that need saving; a TextFile object and a static
I have a basic ActiveRecord model in which i have two fields that i
I have two models. We'll call them object A and object B. Their design

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.