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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:46:48+00:00 2026-05-28T03:46:48+00:00

I have a meta class for the Django User model that I use to

  • 0

I have a meta class for the Django User model that I use to add extra methods (overly simplified version):

# project.models.pie_lover.py

from django.contrib.auth.models import User

class PieLover(User):
    class Meta:
        app_label = "core"
        proxy = True

    def likes_pie(self):
        return True

In my view, I wish to get the logged in PieLover and see if he likes pie (silly thing to do because a PieLover always loves pie, but in a real world situation this may not be the case). My problem lies in the way Django logs in the users, I use the built-in login(request) function for this and as a result the object stored in request.user is a User object, not a PieLover.

# project.views.like_pie.py

from ..models.pie_lover import PieLover

def index(request):
    pie_lover = request.user

    if pie_lover.likes_pie():
        print "Try pie, try!"
    else:
        print "BLASPHEMER!"

If I try to do this Django tells me that the User object has no method likes_pie which is to be expected as request.user is not a PieLover instance.

As a quick workaround I just get the PieLover which has the same ID as the User but that means an extra DB hit.

How can I make Django use PieLover by default in the request?
I was thinking that instead of making another database query to get the proper PieLover object to create a new PieLover object and pass request.user to it at initialization but I don’t know what the implications of this are.

  • 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-28T03:46:49+00:00Added an answer on May 28, 2026 at 3:46 am

    After poking around I found, what seems to me, the easiest and non-hackish way to access the PieLover methods for a given User instance. I have added this to a custom middleware:

    from models.pie_lover import PieLover
    
    class PieLoverMiddleware(object):
        def process_request(self, request):
            request.pie_lover = PieLover(request.user)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
So I have a few Django models that look like this: class City(models.Model): name
I have a property in a Django Model that I'd like to expose via
I have an application that makes use of Django's UserProfile to extend the built-in
I have a Model as follows: class TankJournal(models.Model): user = models.ForeignKey(User) tank = models.ForeignKey(TankProfile)
I have a four classes: class A(models.Model): field1 = models.BooleanField(default = True) class Meta:
I have the following model: from django.db import models class State(models.Model): name = models.CharField(max_length=30)
I have a class Book : from django.db import models from users.models import User
I have a django project that I have been using for a while, but
1] I have the following model: class UserReportedData(db.Model): #country selected by the user, this

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.