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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:37:28+00:00 2026-05-30T23:37:28+00:00

I have two apps, account and myapp. I’m trying to display in a view

  • 0

I have two apps, “account” and “myapp”. I’m trying to display in a view only those teacher objects which belong to the same organisation as the request.user.

account/models.py

from django.contrib.auth.models import User

class Organisation(models.Model):
    name = models.CharField(max_length=100, unique=True)
    is_active = models.BooleanField(default=True)

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)
    organisation = models.ForeignKey(Organisation, editable=False)
    is_organisationadmin = models.BooleanField(default=False)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

Note the last line, from this blog post, which enables accessing user profile information by something like user.profile.organisation

myapp/models.py

from django.contrib.auth.models import User

class Teacher(models.Model):
    user = models.OneToOneField(User, related_name='teacher')

myapp/views.py

from myproject.account.models import Organisation, UserProfile
from myproject.myapp.models import Teacher
from django.contrib.auth.models import User

def homepage(request):
    if request.user.is_authenticated():
        teachers = Teacher.objects.filter(user.profile.organisation == request.user.profile.organisation, user__is_active = True)

I get “NameError at /homepage/, global name ‘user’ is not defined”. I think this is because I’m not correctly accessing the teacher.user property of each Teacher object, but I could be wrong.

I’ve tried all sorts of combinations of reverse traversing the relationship:

user.is_active
user__is_active
user.profile.organisation
user.profile__organisation

but many of the above give me “SyntaxError at /homepage/ keyword can’t be an expression”, so I think the current incarnation is roughly right.

Oddly, the right hand side of the filter seems to work fine (the = request.user.profile.organisation part)

  • 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-30T23:37:30+00:00Added an answer on May 30, 2026 at 11:37 pm

    The documentation on query lookups that span relationships is pretty informative. The thing to realize is that it’s a standard function, so the left-hand side must always be a single keyword, not an expression. To enable this, use the double-underscore syntax:

    Teacher.objects.filter(user__profile__organisation=request.user.profile.organisation, user__is_active = True)
    

    Also note it’s a single = – again, it’s a function invocation, not an expression.

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

Sidebar

Related Questions

I have two apps, both of which force the user to use the iPhone
I have two PHP apps which have twho class with the same name. -
I have two sites in my Apps account and I've got them working for
I have two separate apps (one is classic asp, the other is asp.net) which,
I have two apps between which I want some data exchanged. As they are
I have two apps, which I will refer to as app A and app
I have two apps which are 90% same.So I have used the same code
I have two apps which have same name and same functions. One in android
my end goal is to have two apps, a main app (which will do
I have two apps on Google App Engine, both running under the same account,

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.