簡體   English   中英

Django使用多對多字段更新模型

[英]Django updating a model with a many-to-many field

我正在嘗試為我的網站擔任后期編輯。 當我觸發發布模型時,收到錯誤消息“無法更新模型字段(僅允許非關系和外鍵)。” 更新時。 如何更新此模型?

models.py

class Post(models.Model):
    title = models.CharField(max_length=256)
    disclaimer = models.CharField(max_length=256, blank=True)
    BLOGS = 'blogs'
    APPLICATIONS = 'applications'
    GAMES = 'games'
    WEBSITES = 'websites'
    GALLERY = 'gallery'
    PRIMARY_CHOICES = (
        (BLOGS, 'Blogs'),
        (APPLICATIONS, 'Applications'),
        (GAMES, 'Games'),
        (WEBSITES, 'Websites'),
    )
    content_type = models.CharField(max_length=256, choices=PRIMARY_CHOICES, default=BLOGS)
    screenshot = models.CharField(max_length=256, blank=True)
    tags = TaggableManager()
    body = RichTextField()
    date_posted = models.DateTimeField(default=datetime.now)
    date_edited = models.DateTimeField(blank=True, null=True)
    visible = models.BooleanField(default=True)
    nsfw = models.BooleanField()
    allow_comments = models.BooleanField(default=True)
    files = models.ManyToManyField(File, blank=True)

    def __str__(self):
        if (self.visible == False):
            return '(Hidden) ' + self.title + ' in ' + self.content_type
        return self.title + ' in ' + self.content_type

views.py

class EditPostView(UpdateView):
    form_class = PostForm
    model = Post
    template_name = 'personal/editpost.html'

    def get(self, request, pk):

        if (not request.user.is_superuser):
            return HttpResponseForbidden()

        post = Post.objects.get(id=pk)
        if (post is None):
            return HttpResponseNotFound()

        form = self.form_class(instance=post)

        return render(request, self.template_name, {'form': form, 'post': post})

    def post(self, request, pk):

        if (not request.user.is_superuser):
            return HttpResponseForbidden()

        post = Post.objects.get(id=pk)
        if (post is None):
            return HttpResponseNotFound()

        form = self.form_class(request.POST, instance=post)

        if (form.is_valid()):
            title = form.cleaned_data['title']
            content_type = form.cleaned_data['content_type']
            screenshot = form.cleaned_data['screenshot']
            tags = form.cleaned_data['tags']
            body = form.cleaned_data['body']
            nsfw = form.cleaned_data['nsfw']
            allow_comments = form.cleaned_data['allow_comments']
            files = form.cleaned_data['files'] 

            date_edited = datetime.now()

            Post.objects.filter(id=pk).update(title=title, content_type=content_type,
                                              screenshot=screenshot, tags=tags, 
                                              body=body, nsfw=nsfw, 
                                              allow_comments=allow_comments,
                                              files=files,
                                              date_edited=date_edited)

            return redirect('/posts/' + str(post.id))
        else:
            return HttpResponseNotFound()

        return HttpResponseNotFound()

我會改變:

Post.objects.filter(id=pk).update(title=title, content_type=content_type, screenshot=screenshot, tags=tags, body=body, nsfw=nsfw, allow_comments=allow_comments, files=files, date_edited=date_edited)

至:

post = Post.objects.get(pk=pk)
post.title=title
post.content_type=content_type
post.screenshot=screenshot
post.tags=tags
post.body=body
post.nsfw=nsfw
post.allow_comments=allow_comments
post.files=files
post.date_edited=date_edited
post.save()

看起來文件是一個多對多字段? 然后,您需要創建該字符串的對象並將其添加。 示例代碼:

file = File()
file.name = ""
file.save()    

post = Post.objects.get(pk=pk)
post.title=title
post.content_type=content_type
post.screenshot=screenshot
post.tags=tags
post.body=body
post.nsfw=nsfw
post.allow_comments=allow_comments
post.files.add(file)
post.date_edited=date_edited
post.save()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM