具有ManyToManyField的Django CreateView [英] Django CreateView With ManyToManyField

查看:89
本文介绍了具有ManyToManyField的Django CreateView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Django CreateView,并最终在用户选择某个值的情况下更新ManyToMany字段.我可以验证表单,但最终它不是更新数据库中的manytomanyfield.不知道我在做什么错.我已经引用了类似的SO问题,两个表中的Django ManyToMany CreateView字段但这对我没有帮助.我最近发现了此链接... Django CreateView:如何根据以下内容执行操作保存,这似乎与我遇到的问题非常接近.预先感谢您的任何想法.

I am trying to use a Django CreateView and ultimately update a ManyToMany Field if the user selects a certain value. I can get the form to validate, but ultimately it isn't updating the manytomanyfield in the database. Not sure what I'm doing wrong. I have referenced this similar SO issue, Django ManyToMany CreateView Fields In Both Tables but it was of no help to me. I recently found this link...Django CreateView: How to perform action upon save and it seems very close to the issue I'm having. Thanks in advance for any thoughts.

我的代码:

class AuthorView(LoginRequiredMixin,CreateView):
    model = NewAuthor
    form_class = NewAuthorForm
    template_name = 'create_author.html'

    def form_valid(self, form):
        instance = form.save(commit=False)

        if instance.status == 'Submitted':

            if instance.choice == "Custom":
                instance.access.add(instance.created_by)  
                form.save_m2m()

    return super(CreateAuthorView, self).form_valid(form)

这是我的模特:

class NewAuthor(models.Model):

CHOICES = (
    ('',''),
    ("Default",Default ),
    ("Custom",Custom ),
)

STATUS_CHOICES = (
    ("Submitted","Submitted"),
    ("Pending","Pending")
)

status = models.CharField(choices=STATUS_CHOICES,default="Submitted",max_length=20)
choice = models.CharField(choices=CHOICES,blank=True,max_length=300)
created_by = models.ForeignKey(User,null=True,on_delete=models.DO_NOTHING,related_name='created_by_user')    
access = models.ManyToManyField(User,related_name="new_author_individual_access")

这将通过表单验证,但不会将created_by值保存在manytomany字段中.我试图合并form.save(commit = False)和后续的form.save(),但这似乎也无济于事.我最终试图有条件地将created_by用户添加到manytomany字段中,但到目前为止还算运气.

This passes form validation, but doesn't save the created_by value in the manytomany field. I have tried to incorporate form.save(commit=False) and a subsequent form.save(), but that doesn't seem to help either. I am ultimately trying to add the created_by user to the manytomany field conditionally, but no luck so far.

作为一个快速更新,如果我将form.save_m2m()添加到我的代码中,它实际上确实将created_by添加到了数据库中……但是我还收到一条错误消息,指出对象没有属性'save_m2m'.它实际上添加了created_by,但由于此错误而退出.现在,如果我添加commit = False,则没有错误,但是数据库也没有更新.

As a quick update, if I add the form.save_m2m() to my code, it actually does add the created_by to the database...but I also get an error that says object has no attribute 'save_m2m'. It actually adds the created_by but then quits because of this error. Now if I add commit=False, no errors but the database doesn't get updated either.

推荐答案

此帖子是我的问题的答案. 保存多对多字段Django表单 ModelForms很棒. ..但显然不是m2m ...那太累了...

This post was the answer to my problem. Save Many-To-Many Field Django Forms ModelForms are great....but not with m2m apparently...That was exhausting...

这篇关于具有ManyToManyField的Django CreateView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆