Django many-to-many query -
hello have following model:
class participation(models.model): workflow_activity = models.foreignkey('workflowactivity') user = models.foreignkey(user) role = models.foreignkey(role) current = models.booleanfield()
this many-to-many 'through' table. want filter instances of workflow_activity
have either 1 of these conditions:
- no users assigned, ie, no entries workflow_activity in participation table
- no current active users, ie, rows workflow_activity in participation table have
current==false
help building query appreciated!
edit:
hey everyone, responses. guess im approaching thing wrong. requirement im building sort of ticketing system has auto assign function assigns users tickets based on selection logic. im running assign function periodic task (using celery), need select tickets unassigned current state of ticket. current boolean used mark if particular user assigned current state of ticket.
any ideas/thoughts on implementing this?
edit2
heres 1 thought of:
class workflowactivity(models.model): ... ... assigned_to = models.manytomanyfield('participation') # older table without current field current = models.manytomanyfield('current') class current(models.model): participant = models.foreignkey('participation')
cringe-worthy know, come with, other options?
for #1, should work:
from django.db.models import count workflowactivity.objects.annotate(users=count('participation__user')).filter(users=0)
unfortunately, there isn't way #2 using built-ins in django's orm. try extending count
type, suggested in ticket 11305.
edit:
if interested in finding workflowactivities
have @ least 1 active user
, can do:
workflowactivity.objects.filter(participation__current=true)
Comments
Post a Comment