首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用graphene-django将旧图像更改为新图像?

如何使用graphene-django将旧图像更改为新图像?
EN

Stack Overflow用户
提问于 2019-12-04 23:30:13
回答 1查看 40关注 0票数 1

我想通过使用石墨烯-django将旧图像更改为新图像。

在我的数据库中,有一张图片的id为1。我想更改图片,这意味着我想创建一张id为1的新图片。

这是我的变异:

代码语言:javascript
复制
class ImageAndSelfMutation(graphene.Mutation):
    class Arguments:
        image = Upload()

    Output = types.EditProfileResponse

    def mutate(self, info, image, **kwargs):
        user = info.context.user
        ok = True
        error = None

        if user.is_authenticated is not None:
            try:
                first_image = models.Photo.objects.get(owner=user, order="first")
                create_image = models.Photo.objects.create(image=image[0], owner=user)

                serializer = serializers.ImageSerializer(first_image, data=create_image)

                if serializer.is_valid():
                    serializer.save(owner=user)

            return types.EditProfileResponse(ok=ok, error=error)
        else:
            error = '로그인이 필요합니다.'
            return types.EditProfileResponse(ok=not ok, error=error)

这段代码创建了一些id为2的新数据,但我不想创建新数据。我想更改%1的图片的id。有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2019-12-23 21:50:24

更改实例的id不是一个好主意。新的id值应该在数据库中按顺序生成。您可以使用新镜像复制前一个实例,然后删除前一个实例,而不是更改id:

代码语言:javascript
复制
previous_id = first_image.id
first_image.id = None
first_image.image = image[0]
first_image.save()
models.Photo.objects.filter(id=previous_id).delete()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59179452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档