我正在通过可浏览的api调用API,但它引发了内部服务器错误,响应代码为500。响应主体说 “ / api / v1 / auth / register中的ProgrammingError 关系“ users_user”的列“ company_domain”不存在 LINE 1:...“姓氏”,“加入日期”,“用户名”,“电子邮件”,“ company_d ...”。
我没有尝试太多。
User model:
class User(AbstractUser):
username = models.CharField(db_index=True,
max_length=50,verbose_name="Username/Company Name",
unique=True, help_text="If you are
company,enter Company name.")
email = models.CharField(db_index=True, max_length=50,
unique=True,verbose_name="Email/Company email",
help_text="If you are company,enter Company
email.")
company_domain = models.CharField(
db_index=True, blank=True, verbose_name="Company domain",
max_length=200)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = UserManager()
def __str__(self):
return self.username
Register Serializer:
class RegisterSerializer(serializers.ModelSerializer):
password = serializers.CharField(
max_length=128,
min_length=8,
write_only=True
)
password2 = serializers.CharField(
style={'input_type': 'password'}, write_only=True)
token = serializers.SerializerMethodField(read_only=True)
expires = serializers.SerializerMethodField(read_only=True)
message = serializers.SerializerMethodField(read_only=True)
class Meta:
model = User
fields = [
'id',
'username',
'email',
'password',
'password2',
'token',
'expires',
'message',
'company_domain'
]
def get_message(self, *args, **kwargs):
'''
Return the successful registeration message
'''
return 'Thank You for registering pls verify your email'
def validate_email(self, value):
email = User.objects.filter(email__iexact=value)
if email.exists():
raise serializers.ValidationError('User email already registered')
return value
def validate_username(self, value):
username = User.objects.filter(username__iexact=value)
if username.exists():
raise serializers.ValidationError('Username already registered ')
return value
def get_token(self, obj):
user = obj
payload = JWT_PAYLOAD_HANDLER(user)
token = JWT_ENCODE_HANDLER(payload)
return token
def validate(self, data):
password = data.get('password')
password2 = data.get('password2')
if password != password2:
raise serializers.ValidationError("Passwords must match")
return data
def get_expires(self, obj):
return timezone.now() + expire_delta - datetime.timedelta(seconds=200)
def create(self, validated_data):
if validated_data.get('company_domain') is not None:
user = User.objects.create(
username=validated_data.get('username'),
email=validated_data.get('email'),
company_domain=validated_data.get('company_domain')
)
else:
user = User.objects.create(
username=validated_data.get('username'),
email=validated_data.get('email'))
user.set_password(validated_data.get('password'))
user.save()
return user
RegisterAPIView:
class RegisterAPIView(generics.CreateAPIView):
queryset = User.objects.all()
serializer_class = RegisterSerializer
permission_classes = [AllowAny, ]
def get_serializer_context(self, *args, **kwargs):
return {'request': self.request}
i expect my api to work