如何修改ModelMultipleChoiceField的选择 [英] How to Modify Choices of ModelMultipleChoiceField

查看:648
本文介绍了如何修改ModelMultipleChoiceField的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些设计的模型:

 类作者(模型):
name = CharField()

class Book(Model):
title = CharField()
author = ForeignKey(作者)

让我们想使用一个ModelForm for Book:

  class BookForm (ModelForm):
class Meta:
model = Book

。但是我们还说我的数据库中有大量的作者,我不想有这么长的多选题。所以,我想要限制在BookForm的ModelMultipleChoiceField作者字段上的查询。我们还要说,直到 __ init __ 才能选择所需的查询器,因为它依赖于一个参数被传递。



这似乎可能是诀窍:

  class BookForm(ModelForm):
class Meta:
model = Book

def __init __(self,letter):
#返回基于字母的查询器
choices = getChoices(letter)
self .author.queryset = choices

当然,如果这只是工作,我不会在这里。这让我有一个AttributeError。 'BookForm'对象没有属性'author'。所以,我也尝试过这样的东西,我试图重写ModelForm的默认字段,然后再设置:

  class BookForm (ModelForm):
author = ModelMultipleChoiceField(queryset = Author.objects.all())

class Meta:
model = Book

def __init__ (self,letter):
choices = getChoices(letter)
self.author.queryset = choices

哪个产生相同的结果。



任何人都知道这是如何做的?

解决方案

表单对象没有将其字段作为属性,您需要查看fields属性,这是一个字典:

  self.fields ['author']。queryset = choices 

如果你想充分了解这里发生了什么,你可能对这个答案 - 这是关于模型,但表单类似。


Let's say I have some contrived models:

class Author(Model):
   name = CharField()

class Book(Model):
   title = CharField()
   author = ForeignKey(Author)

And let's say I want to use a ModelForm for Book:

   class BookForm(ModelForm):
      class Meta:
         model = Book

Simple so far. But let's also say that I have a ton of Authors in my database, and I don't want to have such a long multiple choice field. So, I'd like is to restrict the queryset on the BookForm's ModelMultipleChoiceField author field. Let's also say that the queryset I want can't be chosen until __init__, because it relies on an argument to be passed.

This seems like it might do the trick:

class BookForm(ModelForm):
   class Meta:
      model = Book

   def __init__(self, letter):
      # returns the queryset based on the letter
      choices = getChoices(letter)
      self.author.queryset = choices

Of course, if that just worked I wouldn't be here. That gets me an AttributeError. 'BookForm' object has no attribute 'author'. So, I also tried something like this, where I try to override the ModelForm's default field and then set it later:

class BookForm(ModelForm):
   author = ModelMultipleChoiceField(queryset=Author.objects.all())

   class Meta:
      model = Book

   def __init__(self, letter):
      choices = getChoices(letter)
      self.author.queryset = choices

Which produces the same result.

Anyone know how this is intended to be done?

解决方案

Form objects don't have their fields as attributes, you need to look in the "fields" attribute, which is a dictionary:

self.fields['author'].queryset = choices

If you want to fully understand what's going on here, you might be interested in this answer - it's about Models, but Forms work similarly.

这篇关于如何修改ModelMultipleChoiceField的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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