Search code examples
sqlalchemycolanderdeformcolanderalchemywebsauna

Manage ManyToMany relationship in Websauna Admin Panel


I have a model where a lot of ManyToMany connections. I need a smart way to manage from Admin Panel. When creating a campaign model I need to connect to other models. Does Websauna have something like Django Inline foms?

class Campaign(Base, BaseMixin):
    name = Column(String())
    created_at = Column(UTCDateTime, default=now, nullable=False)
    updated_at = sa.Column(UTCDateTime, onupdate=now)
    owner_id = Column(ForeignKey('users.id'))

    workers = relationship('Worker',
                           secondary=CampaignWorker.__table__,
                           back_populates='campaign')
    recipients = relationship('Recipient',
                              secondary=CampaignRecipient.__table__,
                              back_populates='campaign')
    accounts = relationship('Account',
                            secondary=CampaignAccount.__table__,
                            back_populates='campaign')
    messages = relationship('Message',
                            back_populates='campaign')

    message_deliveries = relationship('MessageDelivery',
                                      secondary=CampaignMessageDelivery.__table__,
                                      back_populates='campaign')

Solution

  • Deform can do inline forms as sequences.

    However, as automatically generating forms from ManyToMany relationships is complex, you need to manually construct the Deform schema.

    You can do this by overriding FormView.create_form() that returns a deform.Form object with your custom schema with sequences.

    Then, you need to also override Edit.save_changes() and/or Add.build_object() that would read incoming appstruct (dict) and create or update objects in ManyToMany relationships.