简体   繁体   English

如何设置一个有选择的字符域 django 或使用哪个小部件?

[英]How to style a charfield which has choices in it django or which widget to use?

Okay so what I really want to do is to style a charfield which has choices from my forms.py modelform class.好的,所以我真正想做的是设置一个 charfield,它可以从我的 forms.py modelform class 中选择。

My models.py has this code it it...我的models.py有这个代码......

from django.db import models
from django.contrib.auth.models import User


# Create your models here.
cat_choices = (
    ("Stationary","Stationary"),
    ("Electronics","Electronics"),
    ("Food","Food"),
)

class Product(models.Model):
    name = models.CharField(max_length=100,null=True)
    category = models.CharField(max_length=100, choices=cat_choices,null=True)
    quantity = models.PositiveIntegerField(null=True)

    class Meta:
        verbose_name_plural = 'Product'
        ordering = ['category']

and my forms.py has....我的 forms.py 有....

from django import forms
from .models import Product

class AddProduct(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['name','category','quantity']

    def __init__(self, *args, **kwargs):
        super(AddProduct,self).__init__(*args, **kwargs)
        self.fields['name'].widget = forms.TextInput(attrs={'type':'text','id':'name','name':'name','class':'form-control','placeholder':'Product Name'})
        # self.fields['category'].widget = forms.Select(attrs={'class':'form-select'})

So I need a solution as to how I can style it or more appropriately which widget to use.所以我需要一个解决方案来解决我如何设置它的样式或更合适地使用哪个小部件。 I have already tried Select and it is not working.我已经尝试过 Select 并且它不起作用。

You should change category in your model to be an IntegerField :您应该将 model 中的category更改为IntegerField

cat_choices = (
    (0, "Stationary"),
    (1, "Electronics"),
    (2, "Food"),
)

class Product(models.Model):
    ...
    category = models.IntegerField(choices=cat_choices, null=True)
    ...

then you can use forms.select那么你可以使用forms.select

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何组合两个嵌套元组并在 Django 的 CharField 选择中使用? - How to combine two nested tuples and use in Django's CharField choices? 当我在不基于 Model 的序列化程序中将 Charfield 中的选项放入时出错 - Django Rest 框架 - Error when I put choices in Charfield in a Serializer which is not based on Model - Django Rest Framework 如何根据model.django中的charfield选项过滤用户 - How to filter users depending on a models.charfield choices in django 我该如何在具有Forms属性的CharField的ModelForm中过滤选择(因此是Select Field) - How do I filter the choices in a ModelForm that has a CharField with the choices attribute (and hence a Select Field) Django:限制模型中的选择。PrimaryKey 的字符域 - Django: Limit choices in a models.Charfield by PrimaryKey 带选择的Django CharField,需要模型实例 - Django CharField w/Choices, model instance needed Django 将一个由 ','(逗号)分隔的 charfield 输出拆分为一个字符串数组。? - Django Split a charfield output which is seperated by ','(comma) into an array of strings.? 如何在Django Select小部件中对选项进行分组? - How to group the choices in a Django Select widget? 带有选择的Django CharField,是否自动将可能的选择添加到syncdb上的数据库或迁移? - Django CharField with choices, auto add possible choices to database on syncdb or migrate? 显示哪个小部件具有焦点 - Display which widget has focus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM