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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:17:20+00:00 2026-05-18T11:17:20+00:00

I have a model (UserProfile) with which I want to record a user’s IP

  • 0

I have a model (UserProfile) with which I want to record a user’s IP address when new user accounts are created.

I’ve begin the process of overriding the Django model’s save() method, but I am totally uncertain how to properly access HttpRequest attributes from within a model. Can any of you guys help? Google was unable to provide a specific answer for me.

  • 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-18T11:17:21+00:00Added an answer on May 18, 2026 at 11:17 am

    You always need to pass on request information from the view to your save-method.
    Consider that saving an instance doesn’t always have to be invoked from a http request (for a simple example think of calling save in the python shell).

    If you need to access the request object within the admin while saving, you can override it’s save_model method:

    def save_model(self, request, obj, form, change):
        # do something with obj and request here....
        obj.save()
    

    But otherwise you always have to pass it on from the view:

    def my_view(request):
        obj = MyClass(ip_address = request.META['REMOTE_ADDR'])
    

    Or to make this easier to re-use, make a method on the model like:

    def foo(self, request):
        self.ip_address = request.META['REMOTE_ADDR']
        self..... = request.....
    

    and call it from your view with obj.foo(request).

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

Sidebar

Related Questions

I have a model Foo which have a ForeignKey to the User model. Later,
In my UserProfile model I would like to have a function which returns either
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
I have this model: class UserProfile(models.Model): (UserProfile description) user = models.ForeignKey(User) image = models.ImageField(upload_to=upload_to)
I have django model: class UserProfile(models.Model): user = models.OneToOneField(User) #other fields When I create
I have a UserProfile model with related_name='profile' for the User FK. Let's say I
I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class
I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have a form to edit a UserProfile which is stored in mysql db.

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.