Django Tastypie在过滤关系时返回多个实例

时间:2015-02-18 01:44:02

标签: python django tastypie

我能够通过过滤器成功查询一对多关系,但不是只使用匹配对象获取一条记录,而是为每个匹配的过滤对象重复相同的记录。

例如,如果一家餐馆有许多与我的过滤器匹配的检查,那么Tastypie会返回每个匹配分数的餐馆实例。

即使它有多个匹配对象,有没有办法让单个记录恢复?

查询

http://test.com:8000/restaurants/api/restaurants/?format=json&onlinereports__insp_score__lte=35

models.py

class Restaurant(models.Model):
    rest_permit = models.IntegerField(primary_key=True, verbose_name='Permit', db_index=True)
    rest_name = models.CharField(max_length=200, verbose_name='Name', db_index=True)

    class Meta:
        ordering = ['rest_name']
        select_on_save = True


class OnlineReport(models.Model):
    insp_rest_permit = models.ForeignKey(Restaurant, null=False, to_field='rest_permit', related_name='onlinereports', db_index=True)
    insp_score = models.DecimalField(verbose_name='Score', decimal_places=2, max_digits=5, db_index=True, null=True)

    class Meta:
        ordering = ['insp_date']
        select_on_save = True

resources.py

class OnlineReportResource(ModelResource):

    class Meta:
        queryset = OnlineReport.objects.all()
        resource_name = 'onlinereports'
        filtering = {
            'insp_score': ALL,
        }

class RestaurantResource(ModelResource):

    class Meta:
        queryset = Restaurant.objects.all()
        resource_name = 'restaurants'
        filtering = {
            'onlinereports': ALL_WITH_RELATIONS,
        }

    onlinereports = fields.ToManyField(
        OnlineReportResource,
        'onlinereports',
        null=True,
        full=True
    )

返回示例:

{
limit: 20,
next: null,
offset: 0,
previous: null,
total_count: 2
},
objects: [
    {
    onlinereports: [
        {
            id: 2526,
            insp_score: "11.00"
        },
        {
            id: 47882,
            insp_score: "-7.00"
        },
        {
            id: 47880,
            insp_score: "94.00"
        }
    ],
    rest_name: "Restaurant A",
    rest_permit: 2037
    },
    {
    onlinereports: [
        {
            id: 2526,
            insp_score: "11.00"
        },
        {
            id: 47882,
            insp_score: "-7.00"
        },
        {
            id: 47880,
            insp_score: "94.00"
        }
    ],
    rest_name: "Restaurant A",
    rest_permit: 2037
    }
]   

期望的回报:

{
limit: 20,
next: null,
offset: 0,
previous: null,
total_count: 1
},
objects: [
    {
    onlinereports: [
        {
            id: 2526,
            insp_score: "11.00"
        },
        {
            id: 47882,
            insp_score: "-7.00"
        },
        {
            id: 47880,
            insp_score: "94.00"
        }
    ],
    rest_name: "Restaurant A",
    rest_permit: 2037
    }
]   

将.distinct()添加到RestaurantResource()查询集中:

class RestaurantResource(ModelResource):

    class Meta:
        queryset = Restaurant.objects.all().distinct()
        resource_name = 'restaurants'
        filtering = {
            'onlinereports': ALL_WITH_RELATIONS,
        }

1 个答案:

答案 0 :(得分:2)

使用distinct()作为您的查询集。这样可以消除查询结果中的重复行。

queryset = OnlineReport.objects.all().distinct()