0

I have this going on in one of my class based views:

if form.is_valid():
            form.save(commit=False)
            form.manager = User.objects.get(username=request.user.username)
            form.save()
            location = Location.objects.get(name=form.cleaned_data['name'])
            user.get_profile().owned_locations.add(location)
            return redirect(reverse('location_manager'))

Yet when I fill out the form, I get the following error:

IntegrityError at /dash/location/add
locationmanager_location.manager_id may not be NULL

This is strange given my models for Location look like this:

class Location(models.Model):
    region = models.ForeignKey(Region)
    manager = models.ForeignKey(User)
    name = models.CharField(max_length=255)
    street_address = models.TextField(blank=True)
    city = models.CharField(max_length=255, blank=True)
    zip_code = models.CharField(max_length=20, blank=True)

And my Forms.py looks like this:

class locationForm(forms.ModelForm):
    class Meta:
        model = Location
        fields = (
            'region',
            'name',
            'street_address',
            'city',
            'zip_code',

        )
        exclude =('manager',)

I cannot seem to figure out what the issue is here. If you are curious why I have the exclude, I followed some advice from Daniel Roseman here

Community
  • 1
  • 1
ApathyBear
  • 9,057
  • 14
  • 56
  • 90

2 Answers2

3

Instead of :

    form.save(commit=False)
    form.manager = User.objects.get(username=request.user.username)
    form.save()

try:

    obj = form.save(commit=False)
    obj.manager = User.objects.get(username=request.user.username)
    obj.save()

Hope these helps!

Albert Tugushev
  • 1,504
  • 13
  • 25
  • Thanks! this definitely worked. I am curious though, why am I making an instance and how does form.save() differ from obj.save()? I am a bit confused why this is working and the previous code wasn't. – ApathyBear Jun 04 '14 at 16:39
  • @ApathyBear, details of this behavior you'll find [here](https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method). – Albert Tugushev Jun 04 '14 at 17:48
2

Your form handling is slightly incorrect, you should do it like this:

if form.is_valid():
            instance = form.save(commit=False)
            instance.manager = User.objects.get(username=request.user.username)
            instance.save()
            location = Location.objects.get(name=form.cleaned_data['name'])
            user.get_profile().owned_locations.add(location)
            return redirect(reverse('location_manager'))
jtiai
  • 611
  • 5
  • 11
  • Thanks! Your answer was correct, too. I am curious though, why am I making an instance and how does form.save() differ from obj.save()? I am a bit confused why this is working and the previous code wasn't – ApathyBear Jun 04 '14 at 16:40
  • 1
    form.save() populates instance from form values. If commit is True form.save() issues instance.save() and returns instance. If commit is False form.save doesn't issue instance.save() but returns (partially) filled instance, which you can modify further and issue instance.save() yourself. – jtiai Jun 05 '14 at 03:49