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