0

我有两个型号

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)

视图.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

问题是我需要使用城市过滤业务..我该怎么做?任何猜测

4

1 回答 1

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 方法来编写自定义过滤器。

于 2013-02-11T14:51:41.203 回答