1

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?

Community
  • 1
  • 1
Dekker1
  • 5,565
  • 25
  • 33

1 Answers1

2

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
    ...
gasman
  • 23,691
  • 1
  • 38
  • 56
  • I'm afraid it wouldn't solve my problem. It might not have been too clear, but I'd like to access the relationship in both admin views. So is there any way to achieve this without having to switch the relationship? – Dekker1 Apr 04 '17 at 08:36
  • 1
    ParentalManyToManyField isn't intended to be edited from both directions - it was designed for page models, where that wouldn't be logically possible (the relationship has to belong to one model or the other, for versioning and moderation workflow). What you're doing is probably closer to a plain ManyToManyField - what happens if you try that? (I can't immediately see a reason why it wouldn't work, but I suspect we've never tested that setup...) – gasman Apr 04 '17 at 10:32
  • That was my initial thought as well, using a `ManyToManyField`, however I get the same field error as with the `ParentalManyToManyField`: `FieldError: Unknown field(s) (aes) specified for B`. – Dekker1 Apr 04 '17 at 11:16
  • 1
    Is there any news on this particular issue? Should I make an issue on GitHub? – Dekker1 Apr 07 '17 at 09:57
  • @Dekker, did you post this issue? – allanberry Nov 17 '17 at 19:25