KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
How to auto set the product category_id to a default value when category is deleted? For example 1 to point to the first category. class Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) content = db.Column(db.Text(), unique=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) atime = db.Column(db.DateTime()) def __init__(self, name, content, category_id): self.name = name self.content = content self.category_id = category_id self.atime = datetime.now() def __repr__(self): return '<Product %r>' % self.id class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) products = db.relationship('Product', backref='category', cascade="all, delete, delete-orphan") def __init__(self, *args, **kwargs): if len(kwargs) > 0: self.name = kwargs['name'] def __repr__(self): return '<Category %r>' % self.name I don't want to use cascade to delete them!
Tags (comma-separated)
Save Edits
Cancel