【发布时间】:2014-06-04 06:53:40
【问题描述】:
我在我的一个基于班级的视图中进行了此操作:
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'))
然而,当我填写表格时,我收到以下错误:
IntegrityError at /dash/location/add
locationmanager_location.manager_id may not be NULL
这很奇怪,因为我的 Location 模型如下所示:
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)
我的 Forms.py 看起来像这样:
class locationForm(forms.ModelForm):
class Meta:
model = Location
fields = (
'region',
'name',
'street_address',
'city',
'zip_code',
)
exclude =('manager',)
我似乎无法弄清楚这里的问题是什么。如果你好奇我为什么要排除,我听从了 Daniel Roseman here 的一些建议
【问题讨论】:
-
你能在这里粘贴完整的堆栈跟踪吗?
标签: python django django-models django-forms django-views