我有两个型号
class Business(Basetable):
name = models.CharField(max_length=120)
slug = models.SlugField(max_length=150)
logo=models.OneToOneField("BusinessLogo",null=True,on_delete=models.SET_NULL)
class Address(models.Model):
business = models.ForeignKey("Business", related_name='biz_address')
address1 = models.CharField(max_length=100,null=True)
address2 = models.CharField(max_length=100,null=True)
state = models.ForeignKey(States,null=True,on_delete=models.SET_NULL)
city = models.ForeignKey(City,null=True,on_delete=models.SET_NULL)
view.py
class BusinessResource(ModelResource):
class Meta:
queryset = Business.objects.filter(status='P').order_by('-id')
resource_name = 'business'
filtering = {
'is_black_business': ALL,
'city': ALL_WITH_RELATIONS,
}
def dehydrate(self,bundle):
if buss_address.city:bundle.data['city'] = buss_address.city.name
else:bundle.data['city'] = ''
// i am getting city here
问题是我需要使用城市过滤业务..我该怎么做?任何猜测
答案 0 :(得分:1)
class BusinessResource(ModelResource):
addresses = fields.ToManyField(AddressResource, 'biz_address')
class Meta:
filtering = {
'addresses': ALL_WITH_RELATIONS,
}
class AddressResource(ModelResource):
city = fields.ToOneField(CityResource, 'city')
class Meta:
filtering = {
'city': ALL_WITH_RELATIONS,
}
第二种方法是通过覆盖build_filters和apply_filters方法来编写自定义过滤器。