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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:36:25+00:00 2026-05-11T21:36:25+00:00

I am using the standard User model (django.contrib.auth) which comes with Django. I have

  • 0

I am using the standard User model (django.contrib.auth) which comes with Django. I have made some of my own models in a Django application and created a relationship between like this:

from django.db import models
from django.contrib.auth.models import User

class GroupMembership(models.Model):
    user = models.ForeignKey(User, null = True, blank = True, related_name='memberships')
    #other irrelevant fields removed from example

So I can now do this to get all of a user’s current memberships:

user.memberships.all()

However, I want to be able to do a more complex query, like this:

user.memberships.all().select_related('group__name')

This works fine but I want to fetch this data in a template. It seems silly to try to put this sort of logic inside a template (and I can’t seem to make it work anyway), so I want to create a better way of doing it. I could sub-class User, but that doesn’t seem like a great solution – I may in future want to move my application into other Django sites, and presumably if there was any another application that sub-classed User I wouldn’t be able to get it to work.

Is the best to create a method inside GroupMembership called something like get_by_user(user)? Would I be able to call this from a template?

I would appreciate any advice anybody can give on structuring this – sorry if this is a bit long/vague.

  • 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-11T21:36:26+00:00Added an answer on May 11, 2026 at 9:36 pm

    First, calling select_related and passing arguments, doesn’t do anything. It’s a hint that cache should be populated.

    You would never call select_related in a template, only a view function. And only when you knew you needed all those related objects for other processing.

    “Is the best to create a method inside GroupMembership called something like get_by_user(user)?”

    You have this. I’m not sure what’s wrong with it.

     GroupMembership.objects.filter( user="someUser" )
    

    “Would I be able to call this from a template?”

    No. That’s what view functions are for.

     groups = GroupMembership.objects.filter( user="someUser" )
    

    Then you provide the groups object to the template for rendering.


    Edit

    This is one line of code; it doesn’t seem that onerous a burden to include this in all your view functions.

    If you want this to appear on every page, you have lots of choices that do not involve repeating this line of code..

    1. A view function can call another function.

    2. You might want to try callable objects instead of simple functions; these can subclass a common callable object that fills in this information.

    3. You can add a template context processor to put this into the context of all templates that are rendered.

    4. You could write your own decorator to assure that this is done in every view function that has the decorator.

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

Sidebar

Related Questions

I need to patch the standard User model of contrib.auth by ensuring the email
I am using standard windows forms controls under C#. I have two controls which
Currently I have a custom tool which generates vanilla-SQL (only using standard SQL), from
I have just written a Word Document creation web application using .NET 3.5 which
I am using standard 3G connection for video streaming. Streaming Media application works fine.
I am using the standard outputcache tag in my MVC app which works great
I'm creating a multiple-tenant application that won't use any of the standard Django Admin
I currently have a model that the user can upload a thumbnail to the
I have a Scouts model that requires two actions in addition to the standard
So, I have a django project that is using jinja2 rendering, and I also

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.