2009-08-18 91 views
1

我有以下2種型號:是否可以在Django註釋中的相關項上過濾?

class Job(models.Model): 
    title = models.CharField(_('title'), max_length=50) 
    description = models.TextField(_('description')) 
    category = models.ForeignKey(JobCategory, related_name='jobs') 
    created_date = models.DateTimeField(auto_now_add=True) 

class JobCategory(models.Model): 
    title = models.CharField(_('title'), max_length=50) 
    slug = models.SlugField(_('slug')) 

這是我在哪裏,在與查詢迄今:

def job_categories(): 
    categories = JobCategory.objects.annotate(num_postings=Count('jobs')) 
    return {'categories': categories} 

的問題是,我只想計算在創建的工作過去30天。然而,我想要返回所有類別,不僅僅是那些具有合格工作的類別。

回答

1

我決定採取不同的方法,並選擇不使用註釋。我向作業模型中添加了一個管理器,該管理器僅返回活動(30天或更少的舊作業),並在JobCategory模型上創建一個屬性,以查詢實例的作業計數。我的模板標籤只是返回所有類別。這是相關的代碼。

class JobCategory(models.Model): 
    title = models.CharField(_('title'), max_length=50, help_text=_("Max 50 chars. Required.")) 
    slug = models.SlugField(_('slug'), help_text=_("Only letters, numbers, or hyphens. Required.")) 

    class Meta: 
     verbose_name = _('job category') 
     verbose_name_plural = _('job categories') 

    def __unicode__(self): 
     return self.title 

    def get_absolute_url(self): 
     return reverse('djobs_category_jobs', args=[self.slug]) 

    @property 
    def active_job_count(self): 
     return len(Job.active.filter(category=self)) 

class ActiveJobManager(models.Manager): 
    def get_query_set(self): 
     return super(ActiveJobManager, self).get_query_set().filter(created_date__gte=datetime.datetime.now() - datetime.timedelta(days=30)) 

class Job(models.Model): 
    title = models.CharField(_('title'), max_length=50, help_text=_("Max 50 chars. Required.")) 
    description = models.TextField(_('description'), help_text=_("Required.")) 
    category = models.ForeignKey(JobCategory, related_name='jobs') 
    employment_type = models.CharField(_('employment type'), max_length=5, choices=EMPLOYMENT_TYPE_CHOICES, help_text=_("Required.")) 
    employment_level = models.CharField(_('employment level'), max_length=5, choices=EMPLOYMENT_LEVEL_CHOICES, help_text=_("Required.")) 
    employer = models.ForeignKey(Employer) 
    location = models.ForeignKey(Location) 
    contact = models.ForeignKey(Contact) 
    allow_applications = models.BooleanField(_('allow applications')) 
    created_date = models.DateTimeField(auto_now_add=True) 

    objects = models.Manager() 
    active = ActiveJobManager() 

    class Meta: 
     verbose_name = _('job') 
     verbose_name_plural = _('jobs') 

    def __unicode__(self): 
     return '%s at %s' % (self.title, self.employer.name) 

和標籤...

def job_categories(): 
    categories = JobCategory.objects.all() 
    return {'categories': categories} 
4

只是一個猜測......但會這樣工作嗎?

def job_categories(): 
    thritydaysago = datetime.datetime.now() - datetime.timedelta(days=30) 
    categories = JobCategory.objects.filter(job__created_date__gte=thritydaysago).annotate(num_postings=Count('jobs')) 
    return {'categories': categories} 

有關生成查詢的更多詳細信息,請參閱「lookups-that-span-relationships」。 嗯...可能需要在那裏獲得所有類別的另一個查詢...

+1

這將返回只有那些在過去30天內創建工作的類別。我也試圖避免返回多個查詢集,因爲這違反了使用註釋的目的。這個函數實際上是作爲模板標籤存在的,我希望在模板層中包含每個類別的計數(即使計數爲0)。謝謝你。 – gsiegman 2009-08-18 13:31:58

+0

是的,我認爲你只需要執行兩個查詢。 – monkut 2009-08-19 00:47:30

相關問題