我有两个模型:JobCategory
和Job
。我想要做的是提供数据的初始视图,以填充Backbone模型的集合,其中仅返回具有Jobs的JobCategories,并且对于每个JobCategory,提供所有活动作业。 / p>
# models.py
class PopulatedJobCategoryManager(models.Manager):
def get_queryset(self):
return super(PopulatedJobCategoryManager,
self).get_queryset().exclude(title='other', jobs__isnull=True)
class JobCategory(models.Model):
def __str__(self):
return self.title
title = models.CharField(max_length=225)
objects = models.Manager()
populated = PopulatedJobCategoryManager()
class ActiveJobManager(models.Manager):
def get_queryset(self):
return super(ActiveJobManager, self).get_queryset().filter(
is_active=True)
class Job(models.Model):
job_category = ForeignKey(JobCategory, related_name='jobs')
slug = models.SlugField(max_length=255)
title = models.CharField(max_length=225)
. . .
is_active = models.BooleanField('Active', default=True)
objects = models.Manager()
active = ActiveJobManager()
# serializers.py
class JobSerializer(ModelSerializer):
class Meta:
model = Job
def get_queryset(self):
return Job.active.all()
class JobCategoryFilter(django_filters.FilterSet):
jobs = django_filters.BooleanFilter(name='jobs', lookup_type='isnull')
class Meta:
model = JobCategory
fields = ['id', 'title', 'jobs']
class JobCategorySerializer(ModelSerializer):
jobs = JobSerializer(many=True)
class Meta:
depth = 1
model = JobCategory
fields = ('id', 'title', 'jobs',)
filter_class = JobCategoryFilter
ordering_fields = ('order',)
def get_queryset(self):
return JobCategory.populated.all()
Django Rest Framework似乎根本不想进行任何过滤,也不想使用我的模型管理器。我想要的输出伪代码是:
[
{'title': '', 'jobs': [
{'title': '', 'slug': '', 'is_active': true, ...},
{'title': '', 'slug': '', 'is_active': true, ...},
{'title': '', 'slug': '', 'is_active': true, ...},
. . .
]
},
. . .
]
我做错了什么?