Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access request in ModelForm for adding request.user as foreign key

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'),
like image 993
Dan1ell Avatar asked Feb 27 '23 07:02

Dan1ell


1 Answers

Here's a shorter version of how I'd write the view these days, just for future eyes. No need for any __init__ subclassing, and no repetition of the form constructor:

@login_required
def create_vehicle(request):
    form = forms.VehicleForm(data=request.POST or None)
    if form.is_valid():
        form.instance.owner = request.user
        form.save()
        return redirect('vehicles')
    return render(request, 'vehicles/vehicle_create.html', {'form': form})
like image 52
SmileyChris Avatar answered Apr 08 '23 14:04

SmileyChris