views:

63

answers:

2

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  A: 

You don't show how you are instantiating the form in your view. You need to remember to actually pass the request in when you do so:

def myview(request):
    form = VehicleForm(instance=whatever, request=request)
Daniel Roseman
+1. Post the code to show how you are instantiating the form.
Manoj Govindan
I am using a generic view, added this above. I should thus use a custom view as per your example?
Yes, you're going to need a custom view here.
Daniel Roseman
A: 

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', )