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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:33:26+00:00 2026-05-27T16:33:26+00:00

I need to set the user that is creating the post in the add

  • 0

I need to set the user that is creating the post in the add view:

@login_required 
def add(request):
    if request.method == 'POST':
        form = BusinessForm(request.POST)
        form.user_id = request.user.id
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('listing.views.detail'), args=(f.id))
    else:
        form = BusinessForm()
    return render_to_response('business/add.html', {'form':form},
        context_instance=RequestContext(request))

I assign the user id form.user_id = request.user.id but when saving, it still gives me an error Column user_id cannot be null

Am I doing something wrong? Thanks

EDIT:

I am excluding the user from the form in the model:

class BusinessForm(ModelForm):
    class Meta:
        model = Business
        exclude = ('user',)

Could that be causing the problem?? How can I work around this?

EDIT 2:

Edited my BusinessForm() class as suggested but did not work:

class BusinessForm(ModelForm):
    class Meta:
        model = Business
        exclude = ('user',)
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        return super(BusinessForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        kwargs['commit']=False
        obj = super(BusinessForm, self).save(*args, **kwargs)
        if self.request:
            obj.user = self.request.user
        obj.save()

Business model

class Business(models.Model):
    name = models.CharField(max_length=200)
    user = models.ForeignKey(User, unique=False)
    description = models.TextField()
    category = models.ForeignKey(Category)
    address = models.CharField(max_length=200)
    phone_number = models.CharField(max_length=10)
    website = models.URLField()
    image = models.ImageField(upload_to='business_pictures',blank=True)
  • 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-27T16:33:27+00:00Added an answer on May 27, 2026 at 4:33 pm

    You don’t have to use init or save overrides for this.

    You’re just setting an attribute on your form and the form doesn’t do anything with it. It doesn’t magically behave like a model instance (your form wouldn’t have a user_id attribute).

    Since your form is a ModelForm, you can simply call save on it with commit=False to get the unsaved instance, set the user, then call save on the instance.

     if request.method == 'POST':
        form = BusinessForm(request.POST)
        if form.is_valid():
            business = form.save(commit=False)
            business.user = request.user
            business.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an application that allows the user to add their own UISwitch and
I need to set the height of every textbox on my form, some of
I'm creating a countdown timer that allows the user to choose a valid time,
I'm creating my own user control that can display n amount of images. Here
I have a application that i am creating in ASP.net i need to be
i'm Creating an application that will be working off a User table, the user
Need set zoom for camera preview...
I need Set collection, where its items will be identified by items class. Something
I need to set the text within a DIV element dynamically. What is the
I need to set up an instance of SQL Server 2005 with SQL_Latin1_General_CP850_Bin as

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.