Featured post
django - Get all object with empty set of related ones -
i've got 2 models:
class content(models.model): content_type = models.foreignkey(contenttype) object_id = models.positiveintegerfield(db_index=true) content_object = generic.genericforeignkey() show = models.booleanfield(default=false) class foo(models.model): rel = generic.genericrelation(content)
and want foo methods that's related content object (there one) has show==true
or doesn't have related object @ all. like:
foo.objects.filter(q(rel__show=true) | q(rel__hasnone=true))
but of course there's nothing hasnone
in django.
is there other way in can accomplish (unfortunately aggregation doesn't work generic relations , can't count items).
ok, think i've answer satisfy of (unfortunately not me).
what need left outer join django doesn't support (all joins declared user inner ones), like:
select *, `foobar_bar`.`show` `show` `foobar_foo` left outer join `foobar_bar` on (`foobar_foo`.`id` = `foobar_bar`.`object_id` , ctype = `foobar_bar`.`content_type_id`) show=true or show=null
i assumed both models in foobar
application , ctype content_type of model foo
. haven't found way such query can like:
select *, `foobar_bar`.`show` `show` `foobar_foo` left outer join `foobar_bar` on (`foobar_foo`.`id` = `foobar_bar`.`object_id` (show=true or show=null) , ctype = `foobar_bar`.`content_type_id`
it's not satisfactory exclusive (could join tuples different ctype basing on object's id) still useful. way such query found @ link text. it'll like:
qs = foo.objects.all() qs.query.join((none, 'foobar_foo', none, none)) qs.query.join(('foobar_foo', 'foobar_bar', 'id', 'object_id'), promote=true) foos. qs = qs.extra(select = {'show': 'foobar_bar.show',}, = "(show=true or show=null) , ctype = `foobar_bar`.`content_type_id`")
generally using query.join((,), promote=true)
gets left query join instead of inner, can pass 1 on argument, less solve problem still useful.
- Get link
- X
- Other Apps
Comments
Post a Comment