【问题标题】:Django Rest fields grouping and customisingDjango Rest 字段分组和自定义
【发布时间】:2017-05-31 22:32:46
【问题描述】:

我有一个像这样的 Django 模型:

class Floor(models.Model):
    name = models.CharField(max_lenght =100)

class Point(models.Model):
    created_at      = fields.UnixDateTimeField(auto_now_add=True)
    updated_at      = fields.UnixDateTimeField(auto_now=True)
    floor           = models.ForeignKey(Floor)
    device          = models.CharField(max_lenght=100)

creates_atupdated_at 只是带有时间戳的自定义字段。

所以,我需要发送 points/?start=x,end=y,timeslice=z 之类的请求,其中 x - 是开始时间戳,y - 结束时间戳,z - 此期间的时间片。例如,如果 x 是一天的开始,y 是当天的和,z 是 3600?我将有 24 个切片并希望拥有像这样的 JSON:

{
    floor_id: floor_id
    slice: first timestamp of first slice 
    count: count of devices in this slice
},
{
    floor_id: floor_id
    slice: first timestamp of second slice 
    count: count of devices in this slice
}, 
...

可能,我需要自定义我的序列化程序,使用 django-filters 并为此编写特殊视图,但我不知道如何将它们组合在一起

UPD:好的,我为 Floor 模型定制了我的序列化程序,现在它看起来像:

class FloorWithTPCountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Floor
        fields = ('id', 'count')

    count = serializers.SerializerMethodField('get_tp_count')

    def get_tp_count(self, obj):
        return obj.trackpoint_set.values('tag').distinct().count()

我没有收到 JSON 之类的:

{
    "id": 28,
    "count": 3
},
{
    "id": 35,
    "count": 1
},

我可以建议,我需要在这个序列化类中获取查询字符串参数,并声明一个在时间片内计算点的方法。那么,如何在序列化程序类中获取 querydict?

【问题讨论】:

  • 你的问题不是很清楚。请求 points/?start=x,end=y,timeslice=z 有什么作用?什么是开始、结束和时间片?
  • 哦,对不起,我更正了。
  • 为了确定,您想获取在开始和结束之间创建的所有点的列表,并在某些时间片中组合在一起,其中时间片是以秒为单位的时间段?
  • 不,我想获取每个时间片中起点和终点之间的点数。

标签: python django django-views django-rest-framework


【解决方案1】:

好的,正如我早些时候建议的那样,关键在于序列化程序的定制。我必须为所需的 JSON 结构声明自定义方法。 这是我的解决方案:

class FloorWithTPCountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Floor
        fields = ('id', 'results')

    results = serializers.SerializerMethodField('get_tp_start')

    def get_tp_start(self, obj):
        query_params = self.context.get("request").query_params
        aggregations = {'hour':3600, 'day':86400, 'week':604800}
        if 'start' in query_params and 'end' in query_params:
            st_tm = int(query_params.get('start'))
            en_tm = int(query_params.get('end'))
            if 'aggregation' in query_params:
                aggregation_value = query_params.get('aggregation')
                aggregation = aggregations.get(aggregation_value)
            else:
                aggregation = en_tm - st_tm
            trackpoint_set = obj.trackpoint_set
            st = [{'count': trackpoint_set.filter(created_at__gte=ts, created_at__lt=ts + aggregation).values(
                'tag').distinct().count(), 'timestamp': ts} for ts in range(st_tm, en_tm, aggregation)]

        else:
            st = None
        return st

当然,它仍然缺少一些查询字符串一致性的检查,但我可以根据需要获取 JSON 格式。 例如,请求

tpfloors/?start=1496188800&end=1496275199&aggregation=day

我可以得到这样的东西:

  {
    "id": 49,
    "results": [
      {
        "count": 3,
        "timestamp": 1496188800
      }
    ]
  }, 

最好的问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多