我有一个连接到AWS上的postgres数据库的项目,但是当我去创建一个用户时,用户没有被保存。我能够使用python manage.py createsuperuser
命令创建一个超级用户。我还可以使用PGAdmin看到超级用户正确地出现在数据库中。
设置.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env("POSTGRES_NAME"),
'USER': env("POSTGRES_USER"),
'PASSWORD': env("POSTGRES_PASSWORD"),
'HOST': env("POSTGRES_HOST"),
'PORT': env("POSTGRES_PORT"),
}
}
DATABASES["default"]["ATOMIC_REQUESTS"] = True
字符串
models.py
class UserManager(BaseUserManager):
def _create_user(self, email, password,first_name,last_name,is_staff, is_superuser, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
now = timezone.now()
email = self.normalize_email(email)
user = self.model(
email=email,
is_staff=is_staff,
is_active=True,
is_superuser=is_superuser,
last_login=now,
date_joined=now,
first_name = first_name,
last_name = last_name,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password, first_name, last_name, **extra_fields):
user = self._create_user(email, password, first_name, last_name, False, False, **extra_fields)
user.save(using=self._db)
return user
# return self._create_user(email, password, first_name, last_name, False, False, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
user=self._create_user(email, password, None, None, True, True, **extra_fields)
user.save(using=self._db)
return user
# Custom user model
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=254, unique=True)
name = models.CharField(max_length=254, null=True, blank=True)
first_name = models.CharField(max_length=254, null=True, blank=True)
last_name = models.CharField(max_length=254, null=True, blank=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
型
serializer.py
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = '__all__'
extra_kwargs = {
'password': {'write_only': True},
'id': {'read_only': True}
}
# called when we create a user
def create(self, validated_data):
password = validated_data.pop('password', None)
# user itself
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
型
views.py
class RegisterAPIView(APIView):
def post(self, request):
data = request.data
print("here data")
print(data)
if data['password'] != data['password_confirm']:
raise exceptions.APIException('Passwords do not match!')
serializer = UserSerializer(data=data)
serializer.is_valid(raise_exception=True)
user_id = serializer.save()
# serializer.save()
print('errors:', serializer.errors)
return Response(serializer.data,status=status.HTTP_200_OK)
型
发布请求
{
"first_name": "firstName",
"last_name": "lastName",
"email": "[email protected]",
"password": "password123",
"password_confirm": "password123"
}
型
回复后
{
"id": 15,
"first_name": "firstName",
"last_name": "lastName",
"email": "[email protected]",
}
型
航站楼
here data
{'first_name': 'firstName', 'last_name': 'lastName', 'email': '[email protected]', 'password': 'password123', 'password_confirm': 'password123'}
errors: {}
型
奇怪的是,即使数据库中没有条目,id也会继续增加。我想说问题出在我的视图和序列化器之间,因为超级用户可以成功创建。
**另一个编辑:**我做了更多的挖掘,发现这适用于我的sqlitedb,似乎是RDS Postgres的一个问题。This issue seems somewhat related
1条答案
按热度按时间ttp71kqs1#
我的错误显然是打开了原子请求。我必须深入研究这个问题。我不明白为什么这会导致它失败。