我正在使用 DRF 创建具有多个附件的用户。创建用户时,您必须上传一个或多个文件。当我更新您加载新文件(没有其他修改字段)的用户时,响应返回旧实例。
我通过在返回实例之前强制序列化程序中的“_prefetched_objects_cache”属性来解决。
setattr(instance, '_prefetched_objects_cache', True)
这是对的吗?你有其他解决方案吗?谢谢
有我的代码
在模型.py
class User(models.Model):
#field of user model
class Attachment(models.Model):
class Meta:
db_table = 'attachment'
path = models.FileField()
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='attachments')
dt_created = models.DateTimeField(auto_now_add=True, verbose_name='Created')
在 serializer.py 中
class AttachmentSerializer(serializers.ModelSerializer):
class Meta:
model = Attachment
fields = '__all__'
class UserSerializer(serializers.ModelSerializer):
attachments = AttachmentSerializer(many=True, read_only=True)
def create(self, validated_data):
user = User.objects.create(**validated_data)
for file_item in self.initial_data.getlist('attachments'):
c = Attachment(path=file_item, user=user)
c.save()
return user
def update(self, instance, validated_data):
for item in validated_data:
if User._meta.get_field(item):
setattr(instance, item, validated_data[item])
c = Attachment(path=self.context['request'].FILES['attachments'], user=instance)
c.save()
instance.save()
setattr(instance, '_prefetched_objects_cache', True)
return instance
在 test.py 中
io = StringIO.StringIO()
io.write('foo')
file = InMemoryUploadedFile(io, None, 'foo.txt', 'text', io.len, None)
file.seek(0)
self.user['attachments'] = [file, file]
data = self.user
response = self.client.post(url, data, format='multipart')
file = InMemoryUploadedFile(io, None, 'foo2.txt', 'text', io.len, None)
file.seek(0)
#url = url of user detail for update
local_user['attachments'].extend(response.data['attachments'])
local_user['attachments'].append(file)
data = local_user
response = self.client.put(path=url, data=data, format='multipart')