Django admin choice field

前端 未结 7 1642
日久生厌
日久生厌 2020-12-18 22:48

I have a model that has a CharField and in the admin I want to add choices to the widget. The reason for this is I\'m using a proxy model and there are a bunch of models tha

相关标签:
7条回答
  • You need to think of how you are going to store the data at a database level. I suggest doing this:

    1. Run this pip command: pip install django-multiselectfield
    2. In your models.py file:

      from multiselectfield import MultiSelectField
      
      MY_CHOICES = (('item_key1', 'Item title 1.1'),
                ('item_key2', 'Item title 1.2'),
                ('item_key3', 'Item title 1.3'),
                ('item_key4', 'Item title 1.4'),
                ('item_key5', 'Item title 1.5'))
      
      class MyModel(models.Model):
            my_field = MultiSelectField(choices=MY_CHOICES)
      
    3. In your settings.py:

       INSTALLED_APPS = (
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.sites',
            'django.contrib.admin',
      
            #.....................#
      
            'multiselectfield',
      )
      
    4. Watch the MAGIC happen!

    Source:

    • https://pypi.python.org/pypi/django-multiselectfield
    0 讨论(0)
提交回复
热议问题