Reputation: 21
I'm having challenge deleting and updating many to many tables, below are the model and code
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()
Here is the method to delete queries, actually it work but it only delete a row, how can I delete multiple rows
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
Upvotes: 0
Views: 840
Reputation: 26245
Well, there's http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#ManyToManyField.remove
But you can also do things like:
JobsCategory.delete().where(
(JobsCategory.job == job_obj) &
(JobsCategory.category == category_obj)).execute()
Upvotes: 1