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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:04:08+00:00 2026-06-12T21:04:08+00:00

I am trying to add preferences to user accounts. Each preference is true/false value

  • 0

I am trying to add preferences to user accounts. Each preference is true/false value and it needs to stay this way.

Since each account might have multiple preferences I would like to know how to loop through them in my templates and have those preferences easily available throughout the app.

Ex. I want to display user’s name and all of the colors they like.

models.py

class UserColors(models.Model):
    white = models.BooleanField(_("White"))
    black = models.BooleanField(_("Black"))

class Account(models.Model):
    user = models.OneToOneField(User, unique=True, verbose_name='user', related_name='account')
    colors = models.ForeignKey('UserColors', null=True)

views.py

class UserView(DetailView):
    context_object_name = 'account'
    template_name = 'detail.html'

    def get_object(self, queryset=None):
        return self.request.user

template.html

user: {% account.user.username %} <br>
colors: 

# the following would be ideal instead of doing multiple ifs in search for true/false values
{% for color in account.colors %}
    color.name
{% endif %}

output

user: userName
colors: white, black
  • 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-12T21:04:14+00:00Added an answer on June 12, 2026 at 9:04 pm

    Update:

    In my original answer, I tried restructuring the data and keeping the values as boolean. It looks like we need to stay closer to the original structure.

    You can loop through the fields on your UserColors model. It’s easier in the View code, as you need getattr:

    def get_context_data(self, request, *args, **kwargs):
        data = super(UserView, self).get_context_data(request, *args, **kwargs)
        acct = data['account'] # Guessing this is here based on your posted template code
        colors = {}
        for field in acct.colors._meta.fields:
            colors[field.name] = getattr(acct.colors, field.name, False)
        data['usercolors'] = colors
        return data
    

    Then in the template:

    {% for k,v in usercolors.items() %}
       {% if v %}
       {{ k }}
       {% endif %}
    {% endfor %}
    

    Original answer:

    You could structure it like this:

    class UserColor(models.Model):
        colors = {
            "WHITE": "WHT",
            "BLACK": "BLK",
        }
    
        color_choices = (
            (colors['WHITE'], "White"),
            (colors['BLACK'], "Black"),
        )
    
        name = models.CharField(_("Color Name"), max_length=3, choices=color_choices)
        value = models.BooleanField(_("Whether they like it"))
        user = models.ForeignKey(User)
    

    Then point one at each user for each color that you want to specify:

    user = User.objects.get(name="me")
    white = UserColor.objects.create(
        color=UserColor.colors['WHITE'], value=False, user=user)
    black = UserColor.objects.create(
        color=UserColor.colors['BLACK'], value=True, user=user)
    

    Use it like this:

    {% for color in user.color_set.all() %}
      {{ color.name }}: {{ color.value }}
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying add data triggers to the default combobox style so each text item
Trying to add email notification to my app in the cleanest way possible. When
Trying to add a dynamic class value to one of my columns in CGridView:
I'm trying to design a MySQL database to store user zipcode preferences for providing
I'm trying to query my database based on user input and I get this
I'm trying to use user-preferences checkbox to show or not to show the notification
I'm trying to add an index to my models, but keep getting this error.
I'm trying to add an arraylist to the preferences of my app... At first
I am building a preferences pane for an app where the user can add
I'm trying to calculate how closely an item matches a user's defined preferences. The

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.