SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы
Список вопросов ПечатьМетки: sqlalchemy python
| RemiZOffAlex Создано: 2017-12-14 09:20:21.100220 Обновлено: 2017-12-14 09:20:21.100220 |
|---|
|
Файл /models/document.py
import datetime
from sqlalchemy import Table, Column, Boolean, Integer, ForeignKey, String, DateTime
from sqlalchemy.orm import relationship
from . import Base
class Document(Base):
"""Документы"""
__tablename__ = "document"
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('document.id'))
title = Column(String)
body = Column(String, default='')
created = Column(DateTime) # Дата создания
updated = Column(DateTime) # Дата обновления
# Связи
# Родитель
parent = relationship("Document", remote_side=[id])
# Дочерние узлы
nodes = relationship(
"Document",
primaryjoin="Document.id==Document.parent_id"
)
def __init__(self, title):
self.title = title
self.created = datetime.datetime.utcnow()
self.updated = datetime.datetime.utcnow()
def __repr__(self):
return "<Document('{}')>".format(self.title)
@property
def parents(self):
item = self
from .. import app
while item.parent:
item = item.parent
app.logger.info(item)
yield item
Отображение списка родителей документа в шаблоне. Часть файла document.html
{% if document.parent %}
<small><i class="fa fa-book"></i>
{% for parent in document.parents|reverse %}
<a href="/document/{{ parent.id }}">{{ parent.title }}</a>
{% if not loop.last %} / {% endif %}
{% endfor %}</small>
<br />
{% endif %}
Конечный результат
|