Skip to content

Instantly share code, notes, and snippets.

@lu911
Last active August 29, 2015 14:00
Show Gist options
  • Save lu911/11035276 to your computer and use it in GitHub Desktop.
Save lu911/11035276 to your computer and use it in GitHub Desktop.
Category Model
class Category(IdMixin, CRUDMixin, db.Model):
__tablename__ = '%s_categories' % TITLE
name = db.Column(db.String(16), nullable=False)
type_code = db.Column(db.SmallInteger, default=ETC_TYPE, nullable=False)
parent_category_id = db.Column(db.Integer, db.ForeignKey('%s.id' % __tablename__))
children = db.relationship('Category', cascade='all',
backref=db.backref('parent_category', remote_side='%s.c.id' % __tablename__))
def __init__(self, name, type_code=None, parent_category_id=None):
self.name = name
self.type_code = type_code
self.parent_category_id = parent_category_id
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self.name)
def __str__(self):
return self.name.encode('utf8')
def __unicode__(self):
return self.name
@property
def type(self):
return CATEGORY_TYPE[self.type_code]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment