最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

django - How to filter data obtained through annotation? - Stack Overflow

programmeradmin1浏览0评论

There are 'images' that are attached to 'objects' through a ForeignKey, they can be several at each 'object'. There are 'subjects' that are also attached to 'objects' through ForeignKey. How to attach 'subject' one image from the 'object', noticed "select=1"? Through annotation, I can get either the number of images or all images.

Options that work but that's not what you need

Subject.objects.filter(object_id__hromada_id=hromada.id, state=3).annotate(image=Count('object__objectimages__image', filter=Q(object__objectimages__select=1)))

or

Subject.objects.filter(object_id__hromada_id=hromada.id, state=3).annotate(image=F('object__objectimages__image'))
class Object(models.Model):
    hromada = models.ForeignKey(Hromady, on_delete=models.CASCADE)
    state = models.IntegerField(choices=SELECT_CHOICES, default=SELECT_CHOICES[1][0])
...

class ObjectImages(models.Model):
    object = models.ForeignKey(Object, on_delete=models.CASCADE)
    image = models.ImageField(upload_to=upload_path_img)
    select = models.BooleanField(default=False)
...

class Subject(models.Model):
    object = models.ForeignKey(Object, on_delete=models.CASCADE)
    state = models.IntegerField(choices=SELECT_CHOICES, default=SELECT_CHOICES[1][0])
...

list subjects in Subject => Object => 1 image in ImageObject (select=1) or None

There are 'images' that are attached to 'objects' through a ForeignKey, they can be several at each 'object'. There are 'subjects' that are also attached to 'objects' through ForeignKey. How to attach 'subject' one image from the 'object', noticed "select=1"? Through annotation, I can get either the number of images or all images.

Options that work but that's not what you need

Subject.objects.filter(object_id__hromada_id=hromada.id, state=3).annotate(image=Count('object__objectimages__image', filter=Q(object__objectimages__select=1)))

or

Subject.objects.filter(object_id__hromada_id=hromada.id, state=3).annotate(image=F('object__objectimages__image'))
class Object(models.Model):
    hromada = models.ForeignKey(Hromady, on_delete=models.CASCADE)
    state = models.IntegerField(choices=SELECT_CHOICES, default=SELECT_CHOICES[1][0])
...

class ObjectImages(models.Model):
    object = models.ForeignKey(Object, on_delete=models.CASCADE)
    image = models.ImageField(upload_to=upload_path_img)
    select = models.BooleanField(default=False)
...

class Subject(models.Model):
    object = models.ForeignKey(Object, on_delete=models.CASCADE)
    state = models.IntegerField(choices=SELECT_CHOICES, default=SELECT_CHOICES[1][0])
...

list subjects in Subject => Object => 1 image in ImageObject (select=1) or None

Share Improve this question edited Feb 4 at 18:47 Patrick Bond asked Feb 4 at 17:11 Patrick BondPatrick Bond 1051 silver badge7 bronze badges 2
  • 1 Can you share the corresponding models, and explain what you want to retrieve? It is not entirely clear to me. – willeM_ Van Onsem Commented Feb 4 at 17:50
  • Added models. Need to add image field to queryset. Maybe there is another way. – Patrick Bond Commented Feb 4 at 21:10
Add a comment  | 

1 Answer 1

Reset to default 1

You could try using a query like this.

queryset = (
    Subject.objects
    .filter(object__hromada_id=hromada.id, state=3)
    .annotate(
        image=models.Subquery(
            ObjectImages.objects
            .filter(object_id=models.OuterRef('object_id'), select=1)
            .values_list('image', flat=True)[:1]
        ),
    )
)

This should give you the expected results - for each subject, select by subquery one image that has select=1 and image.object_id=subject.object_id.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>