0

I have the following class:

class Option(models.Model):    
    default = models.BooleanField(default=False)    
    name = models.CharField(max_length=50)    
    def __unicode__(self):
        return self.name

I have another class which uses it:

class Type(models.Model):
   ''' more properties here ''' 
   options = models.ManyToManyField(Option, blank=True)

The idea would be that in django's admin site I would be able to manually add Option instances to a Type.

Now, I already created 4 instances of Option in the admin site. When I go to the Type configuration, to options, all 4 instances are already there. I'd like that box to be empty and add them manually, because for every instance of Type the list should be different.

How can I do that? Something is telling me that I misunderstood ManyToManyField... :)

EDIT: Adding the ModelAdmin class for Option:

class OptionAdmin(admin.ModelAdmin):
    options = forms.ModelMultipleChoiceField(queryset=Option.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False))
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
transient_loop
  • 5,984
  • 15
  • 58
  • 117

2 Answers2

0

I would remove the "options" instance from your Type model and create a new class:

class Assignment(models.Model):
    type = models.ForeignKey(Type)
    option = models.ForeignKey(Option)

Then I'd create as much assignment rows as needed in order to attach options to a type. However it's quite difficult to manage manually in the admin site…

Bogey Jammer
  • 638
  • 2
  • 8
  • 23
0

The best I could come up with is from this post/question:

Django Admin ManyToManyField

Using

filter_horizontal = ('options')

inside the TypeAdmin class

does what I need!

Community
  • 1
  • 1
transient_loop
  • 5,984
  • 15
  • 58
  • 117