如何在Django中的表单中添加来自模型的额外选项。

huangapple go评论62阅读模式
英文:

How to add additional choices to forms which is taken from a model in django

问题

关于在表单中添加从模型中获取的选项,有可能吗?

forms.py

class BookGenreForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ('genre',)

models.py

class Book(Product):
    GENRE_CHOICES = (
        ('Fantasy', 'Fantasy'),
        ('Sci-Fi', 'Sci-Fi'),
        ('Romance', 'Romance'),
        ('Historical Novel', 'Historical Novel'),
        ('Horror', 'Horror'),
        ('Criminal', 'Criminal'),
        ('Biography', 'Biography'),
    )
    author = models.CharField(max_length=100)
    isbn = models.CharField(max_length=100)
    genre = models.CharField(max_length=100, choices=GENRE_CHOICES)

我关心的是为选择添加附加项,比如按字母顺序排列,按受欢迎程度排序。我可以处理视图的创建。同时,我不希望只是在选择模型中添加这些选项,除非在面板中以后用户没有将受欢迎程度等作为流派添加的选项。

我有一个想法,为每个选项创建单独的forms.Forms,并在其中重新创建选项,但我认为这不是一个好的方式。

英文:

As in the topic is it possible to add choices to the forms that take from the model.

forms.py

class BookGenreForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ('genre',)

models.py

class Book(Product):
    GENRE_CHOICES = (
        ('Fantasy', 'Fantasy'),
        ('Sci-Fi', 'Sci-Fi'),
        ('Romance', 'Romance'),
        ('Historical Novel', 'Historical Novel'),
        ('Horror', 'Horror'),
        ('Criminal', 'Criminal'),
        ('Biography', 'Biography'),
    )
    author = models.CharField(max_length=100)
    isbn = models.CharField(max_length=100)
    genre = models.CharField(max_length=100, choices=GENRE_CHOICES)

I care about adding add-ons for selection, such as alphabetically, by popularity. I can handle the creation of a view. At the same time, I would not like to just add these options in the choices model unless it is possible that later in the panel the user does not have the option to add as a genre of popularity, etc.

I have an idea to make individual forms.Forms for everyone and re-create elections in them but I don't think it would be a pretty way to do it

答案1

得分: 1

你可以创建一个自定义表单字段,以添加额外的选项到现有选项中,如下所示:

class GenreChoiceField(forms.ChoiceField):
    def __init__(self, choices=(), *args, **kwargs):
        super().__init__(choices=choices, *args, **kwargs)
        self.choices += [('popularity', 'By popularity'), ('alphabetical', 'Alphabetically')]

class BookGenreForm(forms.ModelForm):
    genre = GenreChoiceField(choices=Book.GENRE_CHOICES)
    class Meta:
        model = Book
        fields = ['genre',]

在上面的示例中,定义了GenreChoiceField,它继承自ModelChoiceField,并向现有的Book.GENRE_CHOICES添加了两个额外选项。然后,BookGenreForm 使用这个自定义字段来替代流派字段的默认CharField

获取更多信息,请参考来自文档的Creating Custom Fields

英文:

You can create a custom form field that adds additional choices to the existing ones, like so:

class GenreChoiceField(forms.ChoiceField):
    def __init__(self, choices=(), *args, **kwargs):
        super().__init__(choices=choices, *args, **kwargs)
        self.choices += [('popularity', 'By popularity'), ('alphabetical', 'Alphabetically')]

class BookGenreForm(forms.ModelForm):
    genre = GenreChoiceField(choices=Book.GENRE_CHOICES)
    class Meta:
        model = Book
        fields = ('genre',)

In the above example, GenreChoiceField is defined that inherits from ModelChoiceField which adds two extra choices to the existing Book.GENRE_CHOICES. The BookGenreForm then uses this custom field instead of the default CharField for the genre field.

For more information, refer Creating Custom Fields from docs.

huangapple
  • 本文由 发表于 2023年3月7日 01:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定