pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.79k stars 1.57k forks source link

How to do inline editing for manytomany relations? #1734

Open scythargon opened 6 years ago

scythargon commented 6 years ago

Hi! I have two models

class Message(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    message_text = db.Column(db.Text, nullable=True)
    stickers = db.relationship('StickerMessageAssociation', back_populates='message')
    ...

and 

class Sticker(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    file_id = db.Column(db.String(255), nullable=False)
    messages = db.relationship('StickerMessageAssociation', back_populates='sticker')
   ...

and an intermediate model to connect these two:

class StickerMessageAssociation(db.Model):
    __tablename__ = 'message_to_sticker'

    message_id = db.Column(db.Integer, db.ForeignKey('message.id'), primary_key=True)
    sticker_id = db.Column(db.Integer, db.ForeignKey('sticker.id'), primary_key=True)

    message = db.relationship('Message', back_populates='stickers')
    sticker = db.relationship('Sticker', back_populates='messages')

The problem arises when I try to enable support for assigning stickers to the messages in the flask-admin via inline model editing:

class InlineStickerModelForm(InlineFormAdmin):
    form_label = 'Stickers'

    def __init__(self):
        return super(InlineStickerModelForm, self).__init__(StickerMessageAssociation)

class MessageView(ModelView):
    ...
    inline_models = (..., InlineStickerModelForm())

When I add a sticker to a message in the admin panel, which looks like this: image

and try to save the page, I get the following error: image

Tried to hack my way around scaffold_inline_form_models and in a few other directions but no luck so far. Any suggestions would be very much appreciated. Thank you!

git-bone commented 5 years ago

similar issue to https://github.com/flask-admin/flask-admin/issues/1472 ?