IntegrityError user_id 不能为 NULL

发布于 2024-10-08 01:35:15 字数 972 浏览 3 评论 0原文

这是我的 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.userin my views. do i have to pass the "Id" or "PK" as an argument to forms or em i making a mistake

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2024-10-15 01:35:15

您可能正在处理 AnonymousUser 对象,并且 id 将始终设置为 None。如果您正在运行 ./manage.py runserver,请尝试使用 pdb 在失败的语句之前添加这样的一行:

import pdb; pdb.set_trace()

这会将您带入 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:

import pdb; pdb.set_trace()

That will kick you into a REPL where you can step through and explore your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文