在视图中将数据添加到模型窗体的多对多字段 [英] Adding data to many-to-many field of a modelform within a view

查看:139
本文介绍了在视图中将数据添加到模型窗体的多对多字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


name = models.CharField(max_length = 100)

class Entry(models.Model):
$
text = models.CharField(max_length = 100)
person = models.ManyToManyField(Person,blank = True,null = True)

class MyModelForm(forms.ModelForm)
class Meta:
model = Entry

在我看来,我需要在保存之前,将pk id添加到提交的表单。

  data = request.POST.copy()
# person'是一个ManyToManyField到'Person'模型
#一个表单通常会以这种格式发送多个id作为POST - > u'id':[u'1',u'2']
#u'1,2'(一个例子)是视图
data [u'person']可访问的str变量= u'1,2'.split(,)
form = MyModelForm(data)
如果form.is_valid():
form.save()

这给了我:


int ()参数必须是字符串或
数字,而不是'list'


哪个是公平的。在以下情况下,它可以工作:

  data [u'person'] = u'1'

我也尝试过没有成功:

  new_form = form.save(commit = False)
new_form.person = u'1,2'.split(,)
new_form.save()
表单。 save_m2m()

如何将多个id保存到此ManyToManyField? br>
必须很简单,但我错过了这一点。






编辑:
所需的结果是MyModelForm的一个新实例(在'entry'表中),所有的id都存储在form.person中('entry_person'表中的多个记录)。






更新:
我似乎孤立了这个问题。



如果我这样做:

  data = {} 
数据[u'person'] = u'1,2'.split(,)

确实有效,结果是:

  {u'person':[u'1 ',u'2'],} 

如果我这样做:

  data = request.POST.copy()
data [u'person'] = u'1,2'.split(,)

不工作(给出上述错误)作为结果是:

 < QueryDict:{u'person':[[u'1',u'2']] }> 

所以我需要的是有

    

任何建议如何?

解决方案

QueryDict .setlist(key,list_)解决了这个问题。



Django QueryDict在列表中包装值的解决方法


class Person(models.Model):
    name = models.CharField(max_length=100)

class Entry(models.Model):
    text = models.CharField(max_length=100)
    person = models.ManyToManyField(Person, blank=True, null=True)

class MyModelForm(forms.ModelForm):
    class Meta:
        model = Entry

In my view, I need to add pk id's to a submitted form before saving it.

data = request.POST.copy()
# 'person' is a ManyToManyField to a 'Person' model
# a form would normally send multiple id's as POST in this format -> u'id': [u'1', u'2']
# u'1,2' (an example) is a str variable accessible to the view
data[u'person'] = u'1,2'.split(",") 
form = MyModelForm(data)
if form.is_valid():
    form.save()

This gives me:

int() argument must be a string or a number, not 'list'

Which is fair enough. It does work in case of:

data[u'person'] = u'1'

I also tried this with no success:

new_form = form.save(commit=False)
new_form.person = u'1,2'.split(",")
new_form.save()
form.save_m2m()

How can I save multiple id's to this ManyToManyField?
Must be easy but I am missing the point.


EDIT: The desired result is one new instance of MyModelForm (in the 'entry' table) with all id's stored for form.person (multiple records in the 'entry_person' table).


UPDATE: I seem to have isolated the problem.

If I do:

data = {}  
data[u'person'] = u'1,2'.split(",")

It does work as the result is:

{u'person': [u'1', u'2'],}  

If I do:

data = request.POST.copy()  
data[u'person'] = u'1,2'.split(",")

It does NOT work (gives the error described above) as the result is:

<QueryDict: {u'person': [[u'1', u'2']],}>

So all I need is to have

<QueryDict: {u'person': [u'1', u'2'],}>

Any suggestions how?

解决方案

QueryDict.setlist(key, list_) solves this problem.

Answered in A workaround for Django QueryDict wrapping values in lists?

这篇关于在视图中将数据添加到模型窗体的多对多字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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