Commit 454728d5ce66d7d5daff927f0c463a3685c162e8

Authored by hitier
1 parent d6b9daca

Use sqlalchemy relationship

category is parent and label is child.
easier to add categories and its labels
Showing 2 changed files with 6 additions and 3 deletions   Show diff stats
app/commands/commands.py
... ... @@ -200,10 +200,10 @@ def feed_from_irap(csv_file_name):
200 200 print("Adding category ", c)
201 201 n_c = Category(name=c)
202 202 db.session.add(n_c)
203   - db.session.commit()
  203 + # db.session.commit()
204 204 for label in l:
205   - print("Adding label {} for id {}".format(label, n_c.id))
206   - n_l = CategoryLabel(name=label, category_id=n_c.id)
  205 + # print("Adding label {} for id {}".format(label, n_c.id))
  206 + n_l = CategoryLabel(name=label, category=n_c)
207 207 db.session.add(n_l)
208 208 db.session.commit()
209 209  
... ...
app/models.py
1 1 from flask_sqlalchemy import SQLAlchemy
  2 +from sqlalchemy.orm import relationship
2 3  
3 4 db = SQLAlchemy()
4 5  
... ... @@ -6,12 +7,14 @@ db = SQLAlchemy()
6 7 class Category(db.Model):
7 8 id = db.Column(db.Integer, primary_key=True)
8 9 name = db.Column(db.String(32))
  10 + labels = relationship("CategoryLabel", back_populates='category')
9 11  
10 12  
11 13 class CategoryLabel(db.Model):
12 14 id = db.Column(db.Integer, primary_key=True)
13 15 name = db.Column(db.String(32))
14 16 category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
  17 + category = relationship("Category", back_populates='labels')
15 18  
16 19  
17 20 class AgentBap(db.Model):
... ...