我收到一个非常奇怪的错误,我不知道是什么原因引起的。 我只想使用Facebook或使用AllAuth(第三方注册应用程序)进行现场注册进行简单注册。
不知何故,每当我尝试注册新用户时,都会创建一个新ID,同时也会创建一个新的USER_ID。但它们不一样(超级用户Id = 1),一个似乎总是均匀而另一个奇怪。这就像是同时创建了两个新用户,但是缺少一个带来此完整性错误的USER_ID。我甚至尝试DROP我的原始数据库,看它是否有帮助,但问题仍然存在。
我正在使用Django 1.8和Python 3.5
它工作正常,直到我决定对用户模块进行一点扩展,如下所示:
profiles / models.py(使用post_save_user_model_receiver中的一些打印进行更新,有时它会连续两次访问此函数,这非常奇怪,因为每次我都会收到完整性错误)
from django.conf import settings
# from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.utils.encoding import smart_text
UF_CHOICES = ( ('SP', 'SP'), ('RJ', 'RJ'), )
User = settings.AUTH_USER_MODEL # 'auth.User'
class Profile(models.Model):
user = models.OneToOneField(User)
id = models.AutoField(primary_key=True)
uf = models.CharField(max_length = 2, blank=False, default = 'SP', choices = UF_CHOICES, verbose_name = 'UF',)
cidade = models.CharField(max_length=120, null=True, blank=True, default="Berlin")
telefone = models.CharField(max_length = 20, blank=True, null=True, verbose_name = 'Telefone para Contato', default=1000)
def __str__(self):
return smart_text(self.user.username)
def post_save_user_model_receiver(sender, instance, created, *args, **kwargs):
print ('post_save_user_model_receiver')
if user.username:
print('created')
if created:
print('try')
try:
Profile.objects.create(user=instance)
except:
pass
else:
print('not created')
post_save.connect(post_save_user_model_receiver, sender=User)
但是现在我从“user_id”获取此IntegrityError,我没有在任何地方指定(猜测它与AllAuth有关,但不确定)。
我做了一些测试,这很奇怪:
在终端的psql中,我可以看到:
You are now connected to database "hunters" as user "Alex".
hunters=# TABLE profiles_profile;
id | uf | cidade | telefone | user_id
----+----+--------+----------+---------
1 | SP | Berlin | e33 | 1
2 | SP | Berlin | | 2
4 | SP | Berlin | | 3
(3 rows)
在Django Shell中:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from profiles.models import Profile
>>> Profile.objects.all()
[<Profile: alexcampos>, <Profile: alex>, <Profile: alx>]
>>> Profile.objects.get(id=1)
<Profile: alexcampos>
>>> Profile.objects.get(id=2)
<Profile: alex>
>>> Profile.objects.get(id=4)
<Profile: alx>
>>> Profile.objects.get(id=3)
Traceback (most recent call last):
File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 69, in handle
self.run_shell(shell=options['interface'])
File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 61, in run_shell
raise ImportError
ImportError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/db/models/query.py", line 334, in get
self.model._meta.object_name
profiles.models.DoesNotExist: Profile matching query does not exist.
>>>
上面我尝试通过Id获取所有对象以找出id = 3,id = 5,依此类推,不存在。只发现偶数和超级用户。
和错误消息:
IntegrityError at /accounts/signup/
null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (5, SP, Berlin, 3332211, null).
上面,5是ID,null是USER_ID。
如果有人知道如何解决这个问题,或者只是可能造成的问题,我会非常感激。
谢谢!