Django: how to exclude the bogus option '----' from select element generated from modal form field?

后端 未结 2 959
挽巷
挽巷 2020-12-21 19:07

models.py:

class Foo(models.Model):
    ...
    TIME_UNIT_TYPE = (
        (\'D\', \'Day\'),
        (\'W\', \'Week\'),
        (\'M\', \'Month\'),
    )
            


        
相关标签:
2条回答
  • 2020-12-21 19:18

    Try with:

    from django.forms import ModelForm
    from django import forms as forms
    
    class FooForm(ModelForm):
        time_unit = forms.forms.TypedChoiceField( 
                        required=True,
                        choices = Foo.TIME_UNIT_TYPE
                    )    
        class Meta:
            model = Foo
            fields = (time_unit,)
    
                  
    

    Test if this works for you.

    0 讨论(0)
  • 2020-12-21 19:26

    None that are particularly easier/less code. You could alternatively create your own Field for your time_unit, extend the _get_choices() method of the default ChoiceField and use it on your time_unit model field if you thought that was cleaner but that's much more work

    0 讨论(0)
提交回复
热议问题