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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:32:00+00:00 2026-06-10T14:32:00+00:00

Like if I have a Message model like class Message(models.Model): from = models.ForeignKey(User) to

  • 0

Like if I have a Message model like

class Message(models.Model):
    from = models.ForeignKey(User)
    to = models.ManyToManyField(User)

    def get_authenticated_user_inbox_messages(self):
        return Message.objects.filter(to=authenticated_user).all()

authenticated_user being a way to get the currently authenticated user.

In the view, I can get the authenticated user with request.user

But, if I don’t want or can’t by design to pass this as a parameter, is there a way to get it directly from inside the model?

Any ideas? Thank you very much!

  • 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-10T14:32:01+00:00Added an answer on June 10, 2026 at 2:32 pm

    No, this is not possible. A Model object exists independently from a web request. For example, you can define a management command that uses model objects from the command-line, where there is no request.

    What I’ve seen as a good practice is to define a static method on your Model object that takes a User object as input, like this:

    class Message(models.Model):
        from = models.ForeignKey(User)
        to = models.ManyToManyField(User)
    
        @staticmethod
        def get_messages_to(user):
            return Message.objects.filter(to=user)
    
        @staticmethod
        def get_messages_from(user):
            return Message.objects.filter(from=user)
    

    You could also define a custom manager for the model and define these convenience methods there instead.

    EDIT: Okay, technically it is possible, but I wouldn’t recommend doing it. For a method, see Global Django Requests.

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

Sidebar

Related Questions

I have two models class Friend(models.Model): me = models.ForeignKey(User) friend = models.ForeignKey(User) remark =
I have such model: class Place(models.Model): name = models.CharField(max_length=80, db_index=True) city = models.ForeignKey(City) address
Suppose I have this model (not real code): class Message(models.Model): content = models.CharField(...) mentions
I have a Message model class (which inherits from ActiveRecord::Base). For a particular deployment,
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have an Address Model which has a ForeignKey to a Contact Model: class
I have the following model class MyClass { id someRandomString } I would like
urls.py url(r'^/mailing/(?P<pk>\d+)/preview/$', PreView.as_view(), name=preview), models.py class Message(models.Model): # ... other fields ... body =
I have a message model and a user model. my message belongs_to my user

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.