0

I want to create one dynamic field value for my class in Django using PyCharm.

CATEGORY_CHOICES = (
    ('on','one'),
    ('tw','two'),
    ('th','three'),
    ('fo','four'),
    ('fi','five'),
)

class art(models.Model):
    Title=models.CharField(max_length=300)
    Desciption=models.TextField()
    Category=models.CharField(max_length=2, choices=CATEGORY_CHOICES)

I want the category field in my class to take more than one option, maybe two or more.

Any help would be appreciated.

user27818
  • 173
  • 1
  • 8
  • Seems to be duplicate of http://stackoverflow.com/questions/2726476/django-multiple-choice-field-checkbox-select-multiple – Eugene Primako Jan 02 '16 at 13:34
  • Please tell us what you mean by `dynamic`? – kia Jan 02 '16 at 13:35
  • one title maybe to belongs one or more choice,for example first title belong one and two from category choices,second title maybe belong only three from category choices,next title maybe belong one,two,three,four from category choices thank you – user27818 Jan 02 '16 at 13:48

1 Answers1

0

If you want one python model to have multiple categories, then you need django ManyToManyField. Basically one model object could have multiple choices, one choice can also belong to multiple models objects:

class Category(models.Model):
    category_name = models.CharField(max_length=10, unique=True)

class Art(models.Model):
    title = models.CharField(max_length=300)
    description = models.TextField()
    category = models.ManyToManyField('Category', blank=True)

Note that I put unique=True for category_name to avoid creating duplicate categories.

Something not related, you shouldn't use lower fist in model name, and upper first for field name, that's really BAD naming convention and might confuse others who read your code.

Example:

# create your category in code or admin
one = Category.objects.create(category_name='one')
two = Category.objects.create(category_name='two')
three = Category.objects.create(category_name='three')

# create a new art obj
new_art = Art.objects.create(title='foo', description='bar')

# add category to Art obj
new_art.category.add(one)
new_art.category.add(two)

# category for new art obj
new_art_category = new_art.category.all()

# get only a list of category names
category_names = new_art_category.values_list('category_name', flat=True)

# create another Art obj
new_art2 = Art.objects.create(title="test", description="test")
# assign category to new_art2
new_art2.category.add(two)
new_art2.category.add(three)

Django doc for many to many and python pep8 doc.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • class Category is a foreign key in the class art or must define ? – user27818 Jan 02 '16 at 14:07
  • Sorry I don't understand your question, class Category is not a foreign key, it's a `ManyToManyField` as I showed you in my code and I also provided link at the end of my answer. – Shang Wang Jan 02 '16 at 14:09
  • now the proplem is if i use 3 categorys in category_name then automatically These categorys go all to category in the class art i dont wont that,i want to select some category from category_name – user27818 Jan 02 '16 at 16:50
  • Sorry I have trouble understanding what's your last comment mean. I just edited my answer to provide some more details. Hope that helps. – Shang Wang Jan 02 '16 at 17:05