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

  • SEARCH
  • Home
  • 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
  • 5 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 I have two entities in Doctrine2 that are related to each other, Models\User
I have two models that are associated with each other. Customer has_one :primary_contact And
I have two Django models with are related to each other through a many-to-many
I have two models -- User and Entry -- that are related through a
When two models are related to each other, Doctrine automatically generates foreign key constraints
If I have two models that are very different, is it defeating the purpose
If I have two models that are guaranteed to have a one-to-one correspondence, i.e.
I have a two models that are similar, but not exactly the same. Here's
Let's assume I have two gmdistibution models that i obtained using modeldata1=gmdistribution.fit(data1,1); modeldata2=gmdistribution.fit(data2,1); Now
In my PHP project I have two different models that have a function with

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.