@RedStone

Как обновить many to many таблицы в SQLAlchemy?

Пилю для себя бложик на фласке, сделал добавление постов, не могу понять как мне теперь обновить тэги у поста. Пост и тэги связаны через many to many связь.

модели
tags = db.Table('tags',
	db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
	db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)

class Post(db.Model):
	id = db.Column(db.Integer, primary_key = True)
	title = db.Column(db.String(150), index = True)
	post_preview = db.Column(db.String(500), index = True, unique = True)
	post_body = db.Column(db.Text(), index = True, unique = True)
	date = db.Column(db.DateTime(), index = True)
	tags = db.relationship('Tag', secondary=tags,
	    backref=db.backref('posts', lazy='dynamic'))

	def __repr__(self):
		return '<Title %r>' % (self.title)


class Tag(db.Model):
	id = db.Column(db.Integer, primary_key = True)
	text = db.Column(db.String(50), index = True)

	def __repr__(self):
		return self.text

	def __init__(self, txt):
		self.text = txt


как обновляю пост
Post.query.filter_by(id = post_id).update({"title":form.title.data, "post_preview":form.post_preview.data, "post_body":form.post_body.data})
  • Вопрос задан
  • 349 просмотров
Пригласить эксперта
Ответы на вопрос 1
@User2017
может это поможет тебе https://techarena51.com/index.php/many-to-many-rel...
Ответ написан
Комментировать
Ваш ответ на вопрос

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

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