@hardwellZero

Как получить id поста?

Здравствуйте.
Дописываю комментарии к постам небольшого блога, не могу получить и внести id поста в таблицу с комментариями.
Что посоветуете?
views.py
@users_blueprint.route('/post/<int:post_id>', methods=['GET', 'POST'])
def post(post_id):
    form = CommentaryForm()
    if form.validate_on_submit():
        commentary = Commentary(
            text = form.text.data,
            key = post_id
        )
        db.session.add(commentary)
        db.session.commit()
        return render_template('post.html')
    post_page = BlogPost.query.filter_by(id=post_id).first()
    return render_template('post.html', post_id=post_id, post_page=post_page, form=form)

models.py
class Commentary(db.Model):

    __tablename__ = "commentary"

    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String, nullable=False)
    writer = db.Column(db.Integer, ForeignKey('users.id'))
    key = db.Column(db.Integer, ForeignKey('posts.id'))

    def __init__(self, text):
        self.text = text
        self.writer = current_user.id

    def __repr__(self):
        return '<text {}'.format(self.text)
  • Вопрос задан
  • 2429 просмотров
Решения вопроса 1
@pcdesign
Ну как-то так:
def __init__(self, text=None, writer=None,  key=None):
        self.text = text
        self.writer = current_user.id
        self.key = key
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы