Reputation: 565
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.
Upvotes: 0
Views: 1693
Reputation: 886
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.
Upvotes: 1