我有一家django oscar商店,并在治疗领域扩展了我的用户模型:
class User(AbstractUser):
treatment = models.CharField(max_length=128)
现在,我想扩展django oscar api以便发布或修补用户(我想更改“待遇”)。
如果我去http://localhost:8000/api/users/并尝试发布一个新的,我只会收到此错误:
TypeError at /api/users/
int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute'
有人知道如何处理吗?
views.py:
class UserList(basic.UserList):
queryset = User.objects.all()
serializer_class = CreateUserSerializer
def post(self, request, format=None):
v_ser = self.serializer_class( data=request.data, context={'request': request})
if v_ser.is_valid():
ser = self.serializer_class( User, context={'request': request})
return Response(ser.data)
return Response(v_ser.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
serializers.py:
class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'email', 'treatment')
email = serializers.EmailField()
treatment = serializers.CharField(max_length=128, required=True)
def validate(self, attrs):
#oscar expects this always to be uppercase.
attrs['treatment'] = attrs['treatment'].upper()
request = self.context.get('request')
return attrs
def create(self, validated_data):
return User.objects.create(**validated_data)