2

我有一个问题,无论如何我都无法解决。

我想要的是,从 api/epg 获得其节目在现在和 2 小时后范围内的频道。不知何故,我得到了所有节目的所有频道,而不是时间范围内的任何东西。

我究竟做错了什么?

谢谢

模型.py

class Channels(models.Model):
    sort_order = models.IntegerField()
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name


class Events(models.Model):

    event_id = models.CharField(max_length=200, primary_key=True)
    channel = models.ForeignKey('Channels', related_name='events')
    start_time = models.DateTimeField( blank=True, null = True)
    end_time = models.DateTimeField( blank=True, null = True)

    def __unicode__(self):
        return self.title

api.py

class EPGResource(ModelResource):
    events= fields.ToManyField('xyz.api.EventResource','events',  full=True)

    class Meta:
        now = datetime.now()
        later = now + timedelta(hours=2)
        queryset = Channels.objects.filter(events__start_time__range(now,later)).order_by('sort_order')
        resource_name = 'epg'
        filtering = {
          "start_time"  :   ['exact', 'range'],
          "name"  :   ['exact', 'range'],
          "events"  :   ['exact', 'range'],
          "channels": ALL_WITH_RELATIONS,
        }



class EventResource(ModelResource):

    class Meta:
        queryset = Events.objects.all()
        resource_name = 'events'

        filtering = {
          "start_time"  :   ['exact', 'range'],
          "genre"  :   ['exact', 'range'],
          "channel": ALL_WITH_RELATIONS,
        }
4

1 回答 1

0

不幸的是,您将需要在运行时进行过滤,get_object_list因为美味的资源不是在运行时构建的:

class EPGResource(ModelResource):
    events= fields.ToManyField('xyz.api.EventResource','events',  full=True)

    class Meta:
        now = datetime.now()
        later = now + timedelta(hours=2)
        queryset = Channels.objects.all()
        resource_name = 'epg'
        filtering = {
          "start_time"  :   ['exact', 'range'],
          "name"  :   ['exact', 'range'],
          "events"  :   ['exact', 'range'],
          "channels": ALL_WITH_RELATIONS,
        }

    def get_object_list(self, request):
        now = datetime.now()
        later = now + timedelta(hours=2)
        return super(EPGResource, self).get_object_list(request).filter(events__start_time__range(now,later)).order_by('sort_order')
于 2013-01-03T04:00:38.237 回答