【问题标题】:How to use a choiceField declared in the model, in a form. django如何在表单中使用模型中声明的choiceField。 django
【发布时间】:2013-04-04 03:59:09
【问题描述】:

我的model.py有这个

class marca(models.Model):
    marcas = (
        ('chevrolet', 'Chevrolet'),
        ('mazda', 'Mazda'),
        ('nissan', 'Nissan'),
        ('toyota', 'Toyota'),
        ('mitsubishi', 'Mitsubishi'),
    )

    marca = models.CharField(max_length=2, choices= marcas)
    def __unicode__(self):
        return self.marca

我需要在我的form.py 中使用它 我试过了,但它不起作用。

class addVehiculoForm(forms.Form):
    placa                   = forms.CharField(widget = forms.TextInput())
    tipo                    = forms.CharField(max_length=2, widget=forms.Select(choices= tipos_vehiculo))
    marca                   = forms.CharField(max_length=2, widget=forms.Select(choices= marcas))

【问题讨论】:

    标签: django django-models django-forms choicefield


    【解决方案1】:

    将您的选择移到模型上方,在 models.py 的根目录中:

    marcas = (
            ('chevrolet', 'Chevrolet'),
            ('mazda', 'Mazda'),
            ('nissan', 'Nissan'),
            ('toyota', 'Toyota'),
            ('mitsubishi', 'Mitsubishi'),)
    
    class Marca(models.Model):
    
        marca = models.CharField(max_length=25,choices=marcas)
    

    然后在您声明表单的文件中:

    from yourapp.models import marcas
    
    class VehiculoForm(forms.Form):
    
         marca = forms.ChoiceField(choices=marcas)
    

    我还为您解决了一些其他问题:

    • 类名应以大写字母开头
    • 您需要增加字符字段的max_length,因为您存储单词chevrolet,只要有人在选择下拉列表中选择Chevrolet

    如果您只是创建一个表单来保存Marca 模型的记录,请使用ModelForm,如下所示:

    from yourapp.models import Marca
    
    class VehiculoForm(forms.ModelForm):
         class Meta:
             model = Marca
    

    现在,django 将自动呈现选择字段。

    【讨论】:

    • forms.CharField 上没有选择。 super(CharField, self).__init__(*args, **kwargs) TypeError: __init__() got an unexpected keyword argument 'choices'
    • 错别字,应该是forms.ChoiceField
    【解决方案2】:

    您需要在模型类 class marca 之外定义选择元组 marcas

    然后你可以在forms.py中做如下使用

    from models import marcas
    
    class addVehiculoForm(forms.Form):
        marca  = forms.CharField(max_length=2, widget=forms.Select(choices= marcas))
        ...
    

    【讨论】:

      【解决方案3】:

      我更喜欢将选项保留在模型中,这样它们就不会与其他模型混在一起。那么解决方案是:

      models.py:

      Same as in question.
      

      forms.py:

      from yourapp.models import marca
      
      class VehiculoForm(forms.Form):
           marca = forms.ChoiceField(choices=marca.marcas)
      
           class Meta:
               model = marca
               fields= ('marca')
      

      如果您想要默认模型字段,您也可以只定义元。

      【讨论】:

        【解决方案4】:

        如果选择(元组)被初始化为私有属性,还有另一种方法

        models.py

        class Marca(models.Model):
            __marcas = (                          #private attributes
                  ('chevrolet', 'Chevrolet'),
                  ('mazda', 'Mazda'),
                  ('nissan', 'Nissan'),
                  ('toyota', 'Toyota'),
                  ('mitsubishi', 'Mitsubishi'),
            )
        
            marca = models.CharField(max_length=100, choices= __marcas)
            def __unicode__(self):
                return self.marca
        

        forms.py

        from yourapp.models import Marca        #import model instead of constant
        
        class VehiculoForm(forms.Form):
            marca = forms.ChoiceField(choices= Marca._meta.get_field('marca').choices)
        
            #If you want to get the default model field
            #or just straight get the base field (not prefer solution)
            marca = Marca._meta.get_field('marca')
        
            #or define in class Meta (prefer and better solution)
            class Meta:
                model = Marca
                fields = ('marca',)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-26
          • 2012-09-18
          • 1970-01-01
          • 2021-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多