我有挑战删除和更新多对多表,下面是模型和代码
class Job(Model):
"""docstring for Jobs"""
url = CharField(max_length=200, unique=True)
title = TextField()
description = TextField()
sponsor = CharField(max_length=100)
location = CharField(max_length=100)
uuid = CharField(max_length=150)
timestamp = DateTimeField(default=datetime.datetime.now)
class Meta:
database = DATABASE
order_by = ('-timestamp',)
class Category(Model):
"""docstring for JobCategory"""
category = CharField(max_length=100, unique=True)
cat_slug = TextField()
jobs = ManyToManyField(Job, related_name='categories')
class Meta:
database = DATABASE
JobsCategory = Category.jobs.get_through_model()
以下是删除查询的方法,实际上它有效,但只删除一行,如何删除多行
def updateEntries(self):
try:
query = models.Job.select().where(models.Job.timestamp < datetime.date.today()-datetime.timedelta(days=2)):
return jobs.execute() # Returns the number of rows deleted. delete_instance()
except models.DoesNotExist:
pass
答案 0 :(得分:1)
那么,http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#ManyToManyField.remove
但你也可以这样做:
JobsCategory.delete().where(
(JobsCategory.job == job_obj) &
(JobsCategory.category == category_obj)).execute()