我对用户注册有一些问题。
我正在使用 Sunscrapers 的 Djoser 库。
在从 Postman 创建新用户期间,我收到“无法创建帐户”响应。
在我的终端(docker)中,我得到以下信息:
ERROR: null value in column "country_id" violates not-null constraint
DETAIL: Failing row contains (53, null, , t, jaroslawpsikuta1@gmail.com, pbkdf2_sha256$180000$p2Q8c4Du3uLF$o56YHGCS0ZutasVhevPXl8rrMG/VYL..., , null, , , , 2020-03-10 10:06:48.053201+00, null, f).
STATEMENT: INSERT INTO "accounts_user" ("last_login", "email", "is_superuser", "is_active", "password", "first_name", "last_name", "date_of_birth", "description", "address", "phone", "created_at", "country_id") VALUES (NULL, 'jaroslawpsikuta1@gmail.com', false, true, 'pbkdf2_sha256$180000$p2Q8c4Du3uLF$o56YHGCS0ZutasVhevPXl8rrMG/VYLiyzTB6LrSY4ws=', '', '', NULL, '', '', '', '2020-03-10T10:06:48.053201+00:00'::timestamptz, NULL) RETURNING "accounts_user"."id"
似乎 Djoser 没有看到我的请求正文,它试图将空值或 null 值放入数据库。到目前为止,我尝试了以下解决方案: 自定义 djoser 创建用户端点 ......但它没有用。
代码:
序列化程序.py
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
class Meta:
model = User
fields = ['id', 'email', 'password', 'first_name', 'last_name', 'date_of_birth', 'description', 'address', 'phone', 'created_at', 'last_login', 'country']
def create(self, validated_data):
password = validated_data.pop('password')
user = User(**validated_data)
user.set_password(password)
user.save()
return user
视图.py
class UserViewSet(ModelViewSet):
serializer_class = UserSerializer
queryset = User.objects.all()
permission_classes = []
您有任何解决方法的想法吗?也许我忘记了什么?谢谢你的帮助:)最好的问候,