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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:35:01+00:00 2026-05-26T17:35:01+00:00

I want a nice convenient attribute to do the following: from django.contrib.auth.models import User

  • 0

I want a nice convenient attribute to do the following:

from django.contrib.auth.models import User
user = User.objects.get(id=2)
user.company
<Company: Big Company L.L.C>

I am currently solving this using lambda. In searching for an answer it looks like perhaps the “right” way to solve this would be to use types.MethodType but I can’t seem to get my head around it. Yes, I have read Raymond excellent guide but I’m clearly missing something.. Here is my current solution for those who are interested..

# Defined Elsewhere
class User:
    name = models.CharField(max_length=32)
class Company(models.Model):
    users =  models.ManyToManyField(User, related_name="companies", blank=True, null=True)

# Here is the meat of this..
class UserProfile(models.Model):
    """This defines Users"""
    user = models.OneToOneField(User)

    def get_company(self):
        try:
            companies = self.user.companies.all()[0]
        except (AttributeError, IndexError):
            return None

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

Right now this works.. But is there a better way – I’m not crazy about lambdas??

  • 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-26T17:35:02+00:00Added an answer on May 26, 2026 at 5:35 pm

    I’m not quite sure I understand correctly what your goal is, but from what I think I understand, it doesn’t seem necessary to do any crazy stuff with descriptors here, let alone types.MethodType. A simple property is fine, and if you don’t like the lambda, you can use an ordinary function decorated with @property:

    class User:
        name = models.CharField(max_length=32)
        @property
        def company(self):
            return UserProfile.objects.get_or_create(user=self)[0].get_company())
    

    Edit: If you can’t touch the User class, you can create a derived class adding the desired property:

    class MyUser(User):
        @property
        def company(self):
            return UserProfile.objects.get_or_create(user=self)[0].get_company())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a nice way to get the current unix timestamp from a java
I want to use an html parser that does the following in a nice,
My app (singe-page layout) receives data from the server and I want a nice
I know it's simple, but I want a nice method. How from url string
I have a simple quiz application and I want display a nice timer /
I want to build a nice API (C#) to make it easier for people
If I were to want to create a nice looking widget to stay running
I want to cleanup some javascript files and reformat them with nice indents, etc,
I have created a nice silverlight control doing exactly what I want it to
Does anyone have a nice solution for GalleryView when I want to display photos

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.