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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:26:18+00:00 2026-05-26T00:26:18+00:00

I have a model that is many-to-one with users. But when I submit a

  • 0

I have a model that is many-to-one with users. But when I submit a form based on that model, I am not correctly providing the user.

class Thing(models.Model):
    """Things."""
    user = models.ForeignKey(User)
    salt = models.IntegerField()
    # [...]

class GenerateThingForm(ModelForm):
    """A form for registering a new Thing."""
    class Meta:
        model = Thing
        fields = (...)    # There are some, but it would distract here

The view says this:

@login_required()
def register_thing(request):
    """Register Thing."""
    if request.method == 'POST': # If the form has been submitted...
        form = GenerateThingForm(request.POST)
        if form.is_valid():
            form.salt = random.getrandbits(32)
            # Apparently I need to specify form.user.
            # Same result if just request.user
            form.user = User.objects.get(request.user)
            form.save()   # Oh, no!  'user_id' is null!

Thanks for any tips.

  • 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-26T00:26:18+00:00Added an answer on May 26, 2026 at 12:26 am

    Don’t set the user or salt on the form instance, it has no idea what to do with it. Set it on the Thing that is generated from form.save.

    thing = form.save(commit=False)
    thing.salt = whatever
    thing.user = myuser
    thing.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Organization model that has_many users through affiliations. And, in the form
I have a model called company that has_many users then users belongs_to company. class
So I have a User model that :has_many other models like Documents, Videos, Posts
Basically, I have a model where I've created a superclass that many other classes
I have a model, Feed, that has and belongs to many FilteredUsers. In this
I have a model that is something like this: class Input(models.Model): details = models.CharField(max_length=1000)
I have a model that has a ForeignKey to the built-in user model in
I have 3 model class User < ActiveRecord::Base has_many :profiles has_many :companies, :through =>
I'm trying to define a many-to-one field in the class that is the Many.
I have an Account model and a User model: class Account < ActiveRecord::Base has_many

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.