简体   繁体   中英

Customize dropdown list from django admin panel

I added a dropdown list on my html file however I want to give an opportunity to some users to edit its content from django admin panel. To do this, I want to put a textbox into the admin panel that adds an item into the dropdown list and when a user write "apple" on the textbox, for example, "apple" will be added into the dropdown list.

Actually what I want to learn is that how to customize a dropdown list? I mean now I have an model that like this;

class Industry(models.Model):
    TYPES = (
        ('apple', 'apple'),
        ('orange', 'orange'),
    )
    type = models.CharField(max_length=30, choices=TYPES)

What I want is that I enable users to put their own TYPES.

Thank you in advance.

If the choices are going to change, use a separate model for the type. That will give you a lot more flexibility.

class Industry(models.Model):
    type = ForeignKey('IndustryType')
    ...

class IndustryType(models.Model):
    type_name = models.CharField(max_length=30)

    def __str__(self):  # use __unicode__ for Python 2
        return self.type_name

The Industry admin form will put a green + next to the type dropdown. That will let you add a new type, which will be added to the dropdown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM