Dekker1
Dekker1

Reputation: 5786

Using reverse (Parental)ManyToManyField in ModelAdmin

Similar to my previous question, I'm trying to use the related model within ModelAdmin. (This is because I would like it to be available in both admin views.) This time, however I am using the new ParentalManyToManyField or just a normal ManyToManyField which seem to mess things up.

I wrote the following structure:

class B(Model): # or Orderable
    ...
    edit_handler = TabbedInterface([
        ObjectList([
            FieldPanel('aes', widget=CheckboxSelectMultiple),
        ], heading=_('Aes'),
        ),
    ])

class A(ClusterableModel):
    ...
    bees = ParentalManyToManyField(
        B,
        related_name='aes',
        blank=True,
    )
    ...
    edit_handler = TabbedInterface([
        ObjectList([
            FieldPanel('bees', widget=CheckboxSelectMultiple),
        ], heading=_('Bees'),
        ),
    ])

When trying to reach the page I receive a Field Error:

Unknown field(s) (aes) specified for B

Is what I'm trying to do not possible yet or did I forget a step?

Upvotes: 1

Views: 631

Answers (1)

gasman
gasman

Reputation: 25292

The ParentalManyToManyField needs to be defined on the parent model (which I assume is meant to be B here - i.e. the modeladmin interface is set up to edit an instance of B with several A's linked to it) and referenced by its field name rather than the related_name. Also, it should be the parent model that's defined as ClusterableModel, not the child:

class B(ClusterableModel):
    aes = ParentalManyToManyField('A', blank=True)

    edit_handler = TabbedInterface([
        ObjectList([
            FieldPanel('aes', widget=CheckboxSelectMultiple),
        ], heading=_('Aes')),
    ])

class A(Model):  # doesn't need to be Orderable, because M2M relations don't specify an order
    ...

Upvotes: 2

Related Questions