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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:06:16+00:00 2026-06-07T03:06:16+00:00

I have a tree of models defined in my Django app (e.g. ‘a’ is

  • 0

I have a tree of models defined in my Django app (e.g. ‘a’ is a top level model, which has many ‘b’s, which has many ‘c’s). I also have the Views/Templates that render these appropriately. For each one of these models I typically need to do a Database query based on the current logged in user.

For example, it’s similar to how each user on stack overflow can mark a question with a star. If my model is the question, I would ask the model if the current user has this question starred and then render it appropriate in the template.

My first thought was to try to pass a parameter in the template (which I now know doesnt’ work).

# template
{{ question.is_starred(request.user) }}    # Can't work.

My second thought was to have some type of global variable (which I don’t like on principle).

# model
class question (Models.model)
    def _is_starred(self):
        # Use a global variable to find out the current logged in user!

My third thought was to have the View tell the model the currently logged in user, but the trouble is, I have a tree of model objects and I think I’d have to load and set every model in the tree, even if I don’t end up using them all. I assume that the objects are lazily loaded.

# view
def view_one_question(request, question_id):
    q = Question.objects.get(pk=question_id)
    q.SetCurrentlyLoggedInUser (request.user.id)
    # !!!! But what about the other network of objects that the question has?
    return render_to_response(...)

Any advice is appreciated. I’m new to Django and trying to start this project off with the best design possible.

  • 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-07T03:06:18+00:00Added an answer on June 7, 2026 at 3:06 am

    Your first example is the right idea, but the wrong implementation. You can’t pass parameters to methods in Django templates. The easiest way around this is a simple filter:

    # yourapp/templatetags/somefile.py
    
    from django import template
    
    register = template.Library()
    
    @register.filter
    def is_starred_for_user(question, user):
        return question.is_starred(user)
    

    Then, in your template:

    {% load somefile %}
    
    {{ question|is_starred_for_user:request.user }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a Tree that has Apples, how should I model the fact
I have category model that has a tree structure. In my database I have
I have a neat Model based off QAbstractItemModel. This has a simple hierarchical tree
I have 2 models: MyModel ( inherits QAbstractItemModel , its tree) and MyProxyModel (
I'm using ExtJs 3.4 and I have a TreeGrid (Ext.ux.tree.TreeGrid) with a column model.
I have a tree in which i want the onOpen and onClose to be
The story so far: I have a rails app with a model named Term.
I am making an android app where I have a bidirectional cyclic object tree
I have two models, Item and ShopSection. They have a many-to-many relationship. @Entity(name =
I have this model: class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) 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.