我的用户注册有一些问题。
我正在使用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看不到我的请求正文,它正在尝试将空值或空值放入数据库。 到目前为止,我尝试了以下解决方案: Customize the djoser create user endpoint ...但是没有用。
代码:
serializers.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
views.py
class UserViewSet(ModelViewSet):
serializer_class = UserSerializer
queryset = User.objects.all()
permission_classes = []
您有任何解决方法的想法吗?也许我忘记了什么?谢谢你的帮助 :) 最好的问候,
答案 0 :(得分:0)
已解决: 我需要在模型中覆盖用户REQUIRED_FIELDS,以使Djoser在序列化程序中使用它。
class User(AbstractUser):
username_validator = None
username = None
...
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'date_of_birth', 'address', 'phone', 'country']