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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:05:56+00:00 2026-05-28T02:05:56+00:00

I have the following models class Book(models.Model): name = models.CharField(max_length=140) class UserProfile(models.Model): favorites =

  • 0

I have the following models

class Book(models.Model):
    name        = models.CharField(max_length=140)

class UserProfile(models.Model):
    favorites    = models.ManyToManyField(Book, null=True, blank=True)
    user         = models.OneToOneField(User)

I need to craete a list of all books and show which ones are favorites and which ones are not.

I need a queryset for a view that gets me all the books like

Book.objects.all()

but i also need to know for each book if it is a favorite for that user and then pass this queryset to the template.

Thanks.

  • 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-28T02:05:57+00:00Added an answer on May 28, 2026 at 2:05 am

    This is a relatively straightforward use of the ManyToManyField.

    class Book(models.Model):
        name        = models.CharField(max_length=140)
    
    class UserProfile(models.Model):
        favorites    = models.ManyToManyField(Book, null=True, blank=True)
        user         = models.OneToOneField(User)
    
    favorite_books = this_user_profile.favorites.all()
    for b in Book.objects.all():
        if b in favorite_books:
            print "Book", b.name, "is a favorite of this user!"
        else:
            print "Book", b.name, "is not a favorite of this user!"
    

    ETA: Since you say you want to add it to the template, give it to the template as a list of tuples.

    book_list = [(b, (b in favorite_books)) for b in Book.objects.all()]
    

    In your template, have the code

    {% for book, is_favorite in book_list %}
        {% if is_favorite %}
            {% comment %} Do something with this favorite book {% endcomment %}
        {% else %}
            {% comment %} Do something with this non favorite book {% endcomment %}
        {% endif %}
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following two models class Questionnaire(models.model) name = models.CharField(max_length=128, null=True, blank=True) type =
I have following models class Artist(models.Model): name = models.CharField(max_length=200, unique=True) photo = models.CharField(max_length=250) views
I have following models relation: class Section(models.Model): section = models.CharField(max_length=200, unique=True) name = models.CharField(max_length=200,
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 Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
I have the following model: class Book(models.Model): title = models.CharField(max_length=100) quantity = models.IntegerField() in_stock
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model): title =
I have the following models: class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField() parent

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.