4

I have two diferents problems with multple choices in models.

The first, i'm trying to do a multiple choice so the user can pick one or more days of the week:

DAYS_CHOICES = (
    (1, _('Monday')),
    ...
    (7, _('Sunday')),
)
...
day = models.ManyToManyField('day', choices=DAYS_CHOICES)

The second problem:

I want to make a ManyToMany Relation with a model define in other model: First (Import to the model):

from events.models import Category

Second (The field related to the model):

type = models.ManyToManyField('Category', null=True, blank=True)

I get this error on syncdb:

Error: One or more models did not validate: situ.situ: 'day' has an m2m relation with model day, which has either not been installed or is abstract.
situ.situ: 'type' has an m2m relation with model Category, which has either not been installed or is abstract.

JMira
  • 888
  • 4
  • 16
  • 34

3 Answers3

5

you could use :

day = forms.ModelMultipleChoiceField(queryset=Day.objects.all())
Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139
Muhammed
  • 51
  • 1
4

Unfortunately, the ManyToMany relation only works for relations with other models, not values from a choices set. Django does not provide a built in multiple choice model field type. However, I have used this snippet in the past when using a multiple select choices field: http://www.djangosnippets.org/snippets/1200/

This encodes the multiple selected options into a comma-separated list stored in a CharField, which works great, unless you need to do some sort of join or something on the selections. If you need to do that, you will have to define a new Day model that you can use the ManyToManyField on.

The second problem, I believe, is just the result of the first--if you clear up that issue, you'll be ok.

Michael C. O'Connor
  • 9,742
  • 3
  • 37
  • 49
2

For the first part of your questions. You should be using a MultipleChoiceField

DAYS_CHOICES = (
    (1, _('Monday')),
    ...
    (7, _('Sunday')),
)
...
days = forms.MultipleChoiceField(choices=DAYS_CHOICES)

http://docs.djangoproject.com/en/dev/ref/forms/fields/#multiplechoicefield

This will yield a list of Unicode objects.

For the second problem, You need to either include the app name in the abstract declaration of the model in the m2m field or not declare it abstractly.

type = models.ManyToManyField(Category, null=True, blank=True)

or

type = models.ManyToManyField('events.Category', null=True, blank=True)

If the Category model was defined later in the same app in the models.py you could leave it Category but since it is in another app, you need to specify the app name.

tutuca
  • 3,444
  • 6
  • 32
  • 54
dting
  • 38,604
  • 10
  • 95
  • 114
  • For the second problem, I changed: "type = models.ManyToManyField('Category', null=True, blank=True)" FOR "type = models.ManyToManyField(Category, null=True, blank=True)" And work fine.For the first problem make a new model, i will try what you said in a few hours, thank you!!! – JMira Mar 04 '11 at 12:30
  • Glad that worked for you. Let me know if you need me to clarify how to use the `MultipleChoiceField`. – dting Mar 04 '11 at 19:52