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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:47:42+00:00 2026-06-01T03:47:42+00:00

I have a form like so: from django import forms from django.contrib.auth.models import User

  • 0

I have a form like so:

from django import forms
from django.contrib.auth.models import User

from django_countries.countries import COUNTRIES

from statuses.models import Status

class StatusForm(forms.Form):
    country = forms.ChoiceField(choices=COUNTRIES)
    mood = forms.IntegerField()
    sleep_quality = forms.IntegerField()

This form is only displayed to the users who are logged in, how can I set request.user so that when the user submits this form, I can associate the form entry to them? My model looks like the following with the the user FK:

from django.db import models
from django.contrib.auth.models import User

from django_countries import CountryField

class Status(models.Model):
    user = models.ForeignKey(User)
    country = CountryField()
    mood = models.SmallIntegerField(default=4)
    sleep_quality = models.SmallIntegerField(default=4)

Here is my view for this form as well:

@login_required
def index(request, template_name="status/index.html"):
    if request.method == 'POST':
        postdata = request.POST
        form = StatusForm(postdata)
        if form.is_valid():
            messages.success(request, 'Something happened, good!')
            return redirect(urlresolvers.reverse('profile'))
    else:
        form = StatusForm()
    context = RequestContext(request, { 'form': form })
    return render_to_response(template_name, context)

I thought maybe I should create a hiddenfield and store request.user in there but that does not seem safe as it can easily be edited with firebug and such. Any suggestions as to how I can store request.user for this form?

Thanks!

  • 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-01T03:47:44+00:00Added an answer on June 1, 2026 at 3:47 am

    The current user will be present in the request as request.user so you don’t need to include it in the form. Instead why not leverage ModelForms as they will deal with linking your object to your form.

    class StatusForm(forms.ModelForm):
        country = forms.ChoiceField(choices=COUNTRIES)
        # The other fields are automatically included, we just overwrite country
    
        class Meta:
            model = Status
            exclude = ("user")
    

    Then in your view:

    ...
    form = StatusForm(request.POST):
        if form.is_valid():
            # Because your model requires that user is present, we validate the form and 
            # save it without commiting, manually assigning the user to the object and resaving
            obj = form.save(commit=False)
            obj.user = request.user
            obj.save()
            messages.success(request, 'Something happened, good!')
            return redirect(urlresolvers.reverse('profile'))
         ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
Currently I have the from django.contrib.auth.models import User but I'm confused as to how
I have a form like: #forms.py from django import forms class MyForm(forms.Form): title =
I have a form in my forms.py that looks like this: from django import
I have a class Book : from django.db import models from users.models import User
I have an application fileman with models.py like so: from django.db import models from
Using django-contact-form , I have overridden the form class: from contact_form.forms import ContactForm from
I have the following model: from django.db import models class State(models.Model): name = models.CharField(max_length=30)
I have a Model and ModelForm like: from django.utils.translation import ugettext_lazy as _ class
I'd like to have any Form managed through the StateMachine generated from LWUIT's 1.5

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.