Question

I'm using Flask-Admin and when I update my content object, it deletes all the items in its associated lookup table (content_products_table).

So if I have a Content object and 7 products mapped in its lookup table (content_products_table) all the mapping from the content to the product is deleted when its updated.

I believe this is happening because my products list is empty while its updating, but in reality my product list could be 1000 items long so I wouldn't want to load it before updating, just simply not update it at all. I feel like there is a way to configure Flask-Admin or sqlalchemy to ignore the field on update.

Example my object below:

content_products_table = db.Table('content_products', db.Model.metadata,
                       db.Column('content_id', db.Integer, db.ForeignKey('content.id')),
                       db.Column('product_id', db.Integer, db.ForeignKey('product.id'))
                       )

class Content(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    products = db.relationship('Product', secondary=content_products_table)

Example of the code thats updating my object:

form.populate_obj(model)
self._on_model_change(form, model, False)
self.session.commit()

Any information would help since I'm new to both of these platforms. Thanks!

EDIT

I wanted to update that when my objects are updated, it deletes all relationships. So if I have a table User who has a One To Many to a table Content. The user_id will be set to null in the Content table when I update the User.

Was it helpful?

Solution

form_excluded_columns = ('products')

Need to explicitly exclude object properties. This seems very dangerous.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top