我是Django的新手,但未找到有关此问题的任何参考。我在Django模型(models.py)中使用多对多字段时遇到此错误。我想问题是在表单(views.py)的view(forms.py)中分配m2m字段。
如何在视图中分配m2m字段?
(Django version 2.0,python - 3.5)
models.py
class User(AbstractUser):
username=models.CharField(max_length=20)
email = models.EmailField(_('email address'), unique=True)
class Setupuser(models.Model):
organization=models.CharField(max_length=200,blank=False,null=True)
emails_for_help = models.ManyToManyField(User)
views.py
class Set_user(FormView):
template_name="pkm_templates/set_up_user.html"
form_class = Set_User_Form
success_url = '/thanks/'
def form_valid(self, form):
org = form.cleaned_data.get('organization')
emails = form.cleaned_data.get("emails_for_help")
instance = Setupuser(organization=org,emails_for_help=emails)
instance.save()
return redirect("/")
forms.py
class Set_User_Form(ModelForm):
emails_for_help = forms.ModelMultipleChoiceField(
queryset=User.objects.all(),
widget=forms.CheckboxSelectMultiple
)
class Meta:
model = Setupuser
fields = ["organization","emails_for_help"]
参考方案
您需要获取User对象,然后将其添加到emails_for_help字段。创建实例时,不能将对象添加到ManyToManyField中。看看doc。
class Set_user(FormView):
template_name="pkm_templates/set_up_user.html"
form_class = Set_User_Form
success_url = '/thanks/'
def form_valid(self, form):
org = form.cleaned_data.get('organization')
emails = form.cleaned_data.get("share_email_with")
users = User.objects.filter(email__in=emails)
instance = Setupuser.objects.create(organization=org)
for user in users:
instance.emails_for_help.add(user)
return redirect("/")
编辑
另一种方法是使用.set()。
class Set_user(FormView):
template_name="pkm_templates/set_up_user.html"
form_class = Set_User_Form
success_url = '/thanks/'
def form_valid(self, form):
org = form.cleaned_data.get('organization')
emails = form.cleaned_data.get("share_email_with")
users = User.objects.filter(email__in=emails)
instance = Setupuser.objects.create(organization=org)
instance.emails_for_help.set(users)
return redirect("/")
或者,您可以简单地使用.add()添加任意数量的对象。
class Set_user(FormView):
template_name="pkm_templates/set_up_user.html"
form_class = Set_User_Form
success_url = '/thanks/'
def form_valid(self, form):
org = form.cleaned_data.get('organization')
emails = form.cleaned_data.get("share_email_with")
users = User.objects.filter(email__in=emails)
instance = Setupuser.objects.create(organization=org)
instance.emails_for_help.add(*users)
return redirect("/")
Python uuid4,如何限制唯一字符的长度 - python
在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…我可以在Python的for循环中使用变量作为索引吗? - python
我在我的python模板上有这个for循环来实现值数组:labels: [{% for bftimes in dataBF.buffers.0.times %} "{{ bftimes }}", {% endfor %}] 我想知道是否可以将int变量用作索引,而不是直接编写它,如上面的代码所示。我需要使用下拉列表的所选值的索引://re…如何更改for循环的索引? - python
假设我有一个for循环:for i in range(1,10): if i is 5: i = 7 如果要满足特定条件,我想更改i。我试过了,但是没用。我该怎么办? 参考方案 对于您的特定示例,这将起作用:for i in range(1, 10): if i in (5, 6): continue 但是,最好使用while循环:i = 1 while i…Python-如何检查Redis服务器是否可用 - python
我正在开发用于访问Redis Server的Python服务(类)。我想知道如何检查Redis Server是否正在运行。而且如果某种原因我无法连接到它。这是我的代码的一部分import redis rs = redis.Redis("localhost") print rs 它打印以下内容
我试图从here在macOS中使用python2创建虚拟环境。在终端中运行pip install virtualenv命令时,出现以下错误。Could not find a version that satisfies the requirement virtualenv (from versions: ) No matching distribution …