我需要在django rest框架中将用户图像添加到内置用户模型的代码。我确实需要用于图像目录的序列化程序,视图和设置的代码 我尝试了几乎所有东西,但完全无法添加它。我给出了用户注册的代码,但没有models.py,因为我使用的是内置用户模型
Views.py
class SignUp(APIView):
def post(self, request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(content, status=status.HTTP_201_CREATED)
Serializers.py
class UserSerializer(serializers.HyperlinkedModelSerializer):
password = serializers.CharField(write_only=True)
password_confirmation = serializers.CharField(read_only=True)
username = serializers.CharField(allow_null=False)
email = serializers.EmailField(allow_null=False)
first_name = serializers.CharField(allow_null=False)
last_name = serializers.CharField(allow_null=False)
class Meta:
model = User
fields = ('id', 'username', 'password', 'password_confirmation', 'email', 'first_name', 'last_name')
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
else:
setattr(instance, attr, value)
instance.save()
return instance
答案 0 :(得分:0)
在您的模型中,您可以使用FileField
或ImageField
upload = models.FileField(upload_to='path/')
photo = models.ImageField(upload_to='path')
有关django模型中字段的更多详细信息,请参阅this_link
答案 1 :(得分:0)
如果您更喜欢简单的方法,那么它会通过OneToOne关系将Image模型与每个User
相关联,
在models.py中,
class Image(models.Model):
user = models.OneToOneField(User)
file = models.ImageField(upload_to='images/')
serializers.py,
class UserSerializer(serializers.HyperlinkedModelSerializer):
password = serializers.CharField(write_only=True)
password_confirmation = serializers.CharField(read_only=True)
avatar = serializers.ImageField(source='image.file')
class Meta:
model = User
fields = ('id', 'username', 'password', 'password_confirmation',
'email', 'first_name', 'last_name', 'avatar',
)
def create(self, validated_data):
password = validated_data.pop('password', None)
# print(validated_data)
image = validated_data.pop('image')
print(image)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
if image is not None:
obj = Image(user=instance, **image)
obj.save()
return instance
def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
elif attr == 'image':
image = Image.objects.get(user=instance)
image.file = validated_data.get('image')['file']
image.save()
else:
setattr(instance, attr, value)
instance.save()
return instance
views.py,
class HomeView(APIView):
serializer_class = UserSerializer
def get(self, request, *args, **kwargs):
users = User.objects.all()
serializer = UserSerializer(users, many=True)
return Response(serializer.data)
def post(self, request, *args, **kwargs):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
向内置User
模型中添加字段将是一项不必要的解决方法。如果你能设法保持附加属性独立于内置模型,那么如果需要重构,它会更可靠。