IntegrityError user_id 不能为 NULL
这是我的 forms.py 文件
def save(self,ip_address, *args, **kwargs):
g = GeoIP()
lat,lon = g.lat_lon(ip_address)
user_location = super(registerForm, self).save()
user_location.latitude = lat
user_location.longitude = lon
user_location.save(*args, **kwargs)
,在我的视图中,我有
def status_set(request):
if request.method == "POST":
rform = registerForm(data = request.POST)
if rform.is_valid():
register = rform.save(ip_address='203.99.178.139')
register.user=request.user
register.save(ip_address)
return render_to_response('home.html')
else:
rform = registerForm()
return render_to_response('status_set.html',{'rform':rform})
,但是当我尝试提交表单时,它显示“IntegrityError:accounts_register.user_id 可能不为 NULL”,但我有 register.user=request.user
在我看来。我是否必须将“Id”或“PK”作为参数传递给表单,或者我犯了一个错误
this is my forms.py file
def save(self,ip_address, *args, **kwargs):
g = GeoIP()
lat,lon = g.lat_lon(ip_address)
user_location = super(registerForm, self).save()
user_location.latitude = lat
user_location.longitude = lon
user_location.save(*args, **kwargs)
and in my views i have
def status_set(request):
if request.method == "POST":
rform = registerForm(data = request.POST)
if rform.is_valid():
register = rform.save(ip_address='203.99.178.139')
register.user=request.user
register.save(ip_address)
return render_to_response('home.html')
else:
rform = registerForm()
return render_to_response('status_set.html',{'rform':rform})
but when i try to submit the forms it says "IntegrityError :accounts_register.user_id may not be NULL"but i have register.user=request.user
in my views. do i have to pass the "Id" or "PK" as an argument to forms or em i making a mistake
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在处理 AnonymousUser 对象,并且 id 将始终设置为 None。如果您正在运行
./manage.py runserver
,请尝试使用 pdb 在失败的语句之前添加这样的一行:这会将您带入 REPL,您可以在其中单步执行并探索您的代码。
You are probably dealing with an AnonymousUser object and the id will always be set to None. If you are running
./manage.py runserver
, try using pdb by putting in a line like this before the statement that is failing:That will kick you into a REPL where you can step through and explore your code.