Reputation: 2319
This is a very simple question, how can I create dropdown field in Django with only specific categories(something similar to the countries dropdown, but not with countries).
Upvotes: 0
Views: 2195
Reputation: 934
With the choice attribute of a Field if it is for fixed values. Or with a ForeignKey field if the Categories should be created dynamicly.
For the ForeignKey field you would do the following:
from django.db import models
class Category(models.Model):
name = models.Charfield(max_length=255)
# ...
def __str__(self):
return self.name
class Item(models.Model):
category = models.ForeignKey(Category)
# ...
Upvotes: 2
Reputation: 1649
Django's most powerful feature is to offer you direct forms. It's a broad question but you want to define a model that can be paired with a form that you can put in a template. Take a look here: https://docs.djangoproject.com/en/1.8/topics/forms/ and here: Django options field with categories and here Creating a dynamic choice field
Upvotes: 1