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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:57:04+00:00 2026-05-17T20:57:04+00:00

I am trying to override save in the modelform to add the current user

  • 0

I am trying to override save in the modelform to add the current user as the owner of a vehicle. But I am receiving ‘NoneType’ object has no attribute ‘user’
What am I forgetting?

forms.py:

class VehicleForm(ModelForm):
    class Meta:
        model = Vehicle
        exclude = ('slug', 'owner', )

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(VehicleForm, self).__init__(*args, **kwargs)

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

my model:

class VehicleBase(models.Model):

    owner           = models.ForeignKey(User)

    vehicle_type    = models.SmallIntegerField(_('kind'),
                  choices=vehicle_types, default=1,)

    make            = models.CharField(_('make'), max_length=31,
                  help_text=_('Maximum is 31 characters.'), )

    model           = models.CharField(_('model'), max_length=31,
                  help_text=_('Maximum is 31 characters.'), )

    class Meta:
        abstract = True


class Vehicle(VehicleBase):
    name        = models.CharField(_('fun name'), max_length=31,
                  help_text=_('Maximum is 31 characters.'), )

    slug            = models.SlugField(_('slug'), )

    def save(self, *args, **kwargs):
        is_new = self.pk is None
        if is_new:
            self.slug = slugify("%s %s %s" %(self.make, self.model, self.name, ))
        super(Vehicle, self).save(*args, **kwargs) # Call the "real" save() method.

        if is_new:
            Calendar.objects.get_or_create_calendar_for_object(self, name = "%s's schedule" %self.name)

    class Meta:
        unique_together = (("name", "owner", ), )

def __unicode__(self):
    return u'%s: %s\'s %s %s' %(self.name, self.owner, self.make, self.model, )

def __str__(self):
    return self.__unicode__()

def get_absolute_url(self):
    return reverse('vehicle_view', kwargs={'object_id':self.id, 'slug':self.slug, }, )

I am using a generic view:

url(r'^create/$',
    'create_update.create_object',
    dict(template_name='vehicles/vehicle_create.html',
         form_class=VehicleForm,
         post_save_redirect="/vehicles/"),
    name='vehicle_create'),
  • 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-17T20:57:05+00:00Added an answer on May 17, 2026 at 8:57 pm

    I wanted to add a little more info to Manoj’s and Daniel’s responses. Basically I needed a custom view in order to pass in the user on save:

    urls.py:

    url(r'^create/$',
        'create_vehicle',
        name='vehicle_create'),
    

    views.py:

    @login_required
    def create_vehicle(request):
        if request.method == 'POST':
            form = VehicleForm(request.POST)
            if form.is_valid():
                vehicle = form.save(commit=False)
                vehicle.owner = request.user
                vehicle.save()
            return HttpResponseRedirect('/vehicles/')
        else:
            form = VehicleForm()
        return render_to_response('vehicles/vehicle_create.html', {'form': form},     context_instance=RequestContext(request))
    

    forms.py:

    class VehicleForm(ModelForm):
        class Meta:
            model = Vehicle
            exclude = ('slug', 'owner', )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to override the Eclipse File > Save menu action to add
I'm trying to override the default User model in Django to add some logic
I am trying to add some fields to the user after the user has
I've been trying to override the file login.xml but no matter what I try,
I am trying a very simple override. I just want to add one line
I'm trying to override the way rails apply and id to an associated object,
I am trying to locate a message which I can override and then save
I'm trying to manually update a db in Spree, but it has no model
I'm trying to override the save_model method on a Django admin object to prevent
I'm trying to save a spinner's selection to a shared preference, but I'm having

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.