Monday, 15 April 2013

django - How to make M2M items unique -



django - How to make M2M items unique -

i creating keyword field table items has unique. understand how create table items unique, how create m2m items unique aswell not reused?

here code have far:

class keyword(models.model): name = models.charfield(max_length=100, unique=true, blank=false) class mainentity(models.model): keywords = models.manytomanyfield(keyword, default=none, null=true, blank=true, verbose_name='keyword')

so illustration works, except fact can create bunch of other mainentity items has same keyword, cannot create duplicate keyword hence unique. how restrict usage of same keyword?

okay, going take stab @ answer. next ensures have unique keywords, can refer single mainentity, mainentity can have multiple keywords:

class keyword(models.model): name = models.charfield(max_length=100, unique=true, blank=false) main_entity = models.foreignkey(mainentity) class mainentity(models.model): ...

(i presume maintentity has other fields have been stripped sake of brevity , you'll replace "..." fields ^_^)

django django-models django-admin

No comments:

Post a Comment