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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:15:51+00:00 2026-06-04T07:15:51+00:00

I have this form.py: from shortcut import get_timezone class HostCreateForm(forms.ModelForm): def save(self, commit=True): host1

  • 0

I have this form.py:

 from shortcut import get_timezone
 class HostCreateForm(forms.ModelForm):
     def save(self, commit=True):
         host1 = super(HostCreateForm, self).save(commit=False)
         user = request.user.username 
         host1.timezone=get_timezone(user=user)
         host1.save()
         return host1

 class Meta:
     model = Host
     widgets = {
         'user': forms.HiddenInput(),
         'timezone': forms.HiddenInput()
     }
    timezone is an input hidden field from Host model defined in model.py.
    get_timezone method: (definition as following)
 def get_timezone(user=None):
 timezone = Userprofile.objects.all()
 timezone = timezone.get(user=user)
 return timezone.timezone

whereas UserProfile is another model also defined in models.py which is used to store the timezone , first name and last name of a user at the time of his/her registration. My main motive is to get this default timezone from Userprofile model and pass it to that hidden input field (timezone).
I tried with the above code (host1.timezone=get_timezone(user=user) written in save method which call the get_timezone method and it should return the timezone form Userprofile model for a loginned user but it didn’t work.

I think the main problem are coming with the passing the name of requesting user at the time of calling get_timezone methos from save method. I am not able to do that. From the current scenario i am getting the following error:
global name ‘request’ is not defined

Any help will be appreciable

  • 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-04T07:15:52+00:00Added an answer on June 4, 2026 at 7:15 am
    timezone = timezone.filter(user=user)
    return timezone.timezone
    

    timezone.filter(...) will return you a queryset, and you can not call .timezone on a queryset. You must get the related object by either using get:

    timezone = timezone.get(user=user)
    

    or by list slicing:

    timezone = timezone.filter(user=user)[0]
    

    UPDATE: Ok, i just notice it.

    Problem is, you can not pass a request object to a form’s save method. Best way to deal with the problem is doing it in your view layer.

    In your view:

    my_form = HostCreateForm(request.POST):
        if my_form.is_valid(): #do the validation
            my_inst = my_form.save(commit=False) #pass the form values to a new model instance without saving.
            my_inst.timezone = request.user.username #set the user or do whhatever you want eith the form data.
            my_inst.save() #that will call your save methhod that you override.
    

    And you remove the parts that sets the user from your save method.

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

Sidebar

Related Questions

i have this form and im trying to get the value from the text
I have this form which allows the input of any product quantity from 1-10:
I have this java servlet that grabs information from a form, I need to
I have this function to edit all fields that come from the form and
I have an array of data that I'm getting from this form (form.php): <?
I have some data scraped and processed from the web in this form: >head(dat)
I have a form and from this I call dialogPrintDiet.ShowDialog() which launchs my dialog.
I have a WebApi service handling an upload from a simple form, like this
I currently have a datasource from a client in the form of XML, this
I have this form: <form action=javascript:; class=iceFrm enctype=application/x-www-form-urlencoded id=frmMainMenu method=post onsubmit=return false;> ..... </form>

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.