2017-01-30 116 views
2

獲取相關的對象,我有以下三種模式:的Django 1.6 - 相關對象

class Region(models.Model): 
    code = models.IntegerField() 

class ReportRequest(models.Model): 
    # a report can include many regions 
    regions = models.ManyToManyField(Region) 

class Office(models.Model): 
    # an office belongs to one region 
    region = models.ForeignKey(Region) 

基本上,許多報道可以有很多地區,一個區域可以有多個辦事處。 如何在一個查詢中獲取與報表區域相關的所有辦公室?

我可以用兩個查詢做到這一點:

region_pks = reportrequest.regions.values_list('id', flat=True) 
offices = Office.objects.filter(region_id__in=region_pks) 

,但我覺得必須有一個辦法在短短一個查詢做到這一點。

我知道如何做到這一點,如果這是一個由ForeignKeys連接的對象鏈,但ManyToManyField混淆了我一下。

更新:

這樣Office.objects.filter(region__in=reportrequest.regions)不起作用。

reportrequest = ReportRequest.objects.first() 
print(Office.objects.filter(region__in=reportrequest.regions).all()) 

Traceback (most recent call last): 
    File "/Users/9999/_projects/declarator/transparency/transparency/scripts/process_report_requests.py", line 22, in <module> 
    print(Office.objects.filter(region__in=reportrequest.regions).all()) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter 
    return self._filter_or_exclude(False, *args, **kwargs) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/modeltranslation/manager.py", line 338, in _filter_or_exclude 
    return super(MultilingualQuerySet, self)._filter_or_exclude(negate, *args, **kwargs) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude 
    clone.query.add_q(Q(*args, **kwargs)) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1287, in add_q 
    clause, require_inner = self._add_q(where_part, self.used_aliases) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1314, in _add_q 
    current_negated=current_negated, connector=connector) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1142, in build_filter 
    value, lookups = self.prepare_lookup_value(value, lookups, can_reuse) 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1047, in prepare_lookup_value 
    value = value() 
    File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/fields/related.py", line 839, in __call__ 
    manager = getattr(self.model, kwargs.pop('manager')) 
KeyError: u'manager' 

回答

1

offices = Office.objects.filter(region__in=reportrequest.regions.all()) 
+0

這看起來合法的,但不起作用。請參閱上面的更新 – kurtgn

+0

根據您的例外。嘗試更新的一個。沒有測試過這 –

+0

是的這個作品 - 謝謝! – kurtgn