Django ORM查询,manytomany自引用左连接和where

时间:2017-10-01 18:42:01

标签: django orm django-queryset

我需要翻译一个查询。

模型

admin/assets/images

SQL查询:

let mix = require('laravel-mix');

mix.setPublicPath('public');
mix.setResourceRoot('../')

mix.js('resources/assets/js/app.js', 'assets/js')
   .sass('resources/assets/sass/app.scss', 'assets/css');

// Admin assets

mix.js('resources/themes/admin/default/assets/js/app.js', 'admin/assets/js')
    .sass('resources/themes/admin/default/assets/sass/app.scss', 'admin/assets/css');

if (mix.inProduction()) {
    mix.version();
}

我需要检索所有不属于Publication_relationship或其publication_dependecy为1的出版物。换句话说,publication_dependency为1或NULL

2 个答案:

答案 0 :(得分:0)

Publication.objects.filter(to_publication__publication_dependecy__isnull=True).filter(to_publication__publication_dependecy__id=1).values_list('id', Flat=True)

这将是您的查询。你是什​​么意思 publication_dependency是1 因为publication_dependency是一个对象而不是整数或charField类型的值,因此我想你引用了id。告诉我,如果有效

答案 1 :(得分:0)

from django.db.models import Q

qs = (Publication.objects
      .filter(Q(to_publication__publication_dependecy__isnull=True) | 
              Q(to_publication__publication_dependecy__id=1))
      .values_list('id', Flat=True)
)