按日期时间类型的列订购查询集

时间:2019-03-14 13:38:36

标签: django django-rest-framework

我的类型为datetime的模型中有一个“时间戳”列。我想按时间戳的降序对queryset进行排序。

OHLCV.objects.all().order_by('-timestamp')
  

TypeError:参数必须为int或float

如何调试此问题?谢谢!

PS:我正在使用DRF,无法在序列化器或视图中订购它(使用django-filter)

编辑:从Django shell追溯

In [74]: OHLCV.objects.all()
Out[74]: <QuerySet [<OHLCV: OHLCV object (5fa8629c-622b-4ab5-9920-615c979c431f)>, <OHLCV: OHLCV object (f0f7dd7e-705
e-428e-b01d-26c5b554b2fd)>, <OHLCV: OHLCV object (ab72a3c5-8df5-4136-8a90-19e91bbae709)>, <OHLCV: OHLCV object (d257
9688-7da0-46b7-9ad3-ddcfaeb2532f)>, <OHLCV: OHLCV object (8e3a0875-14df-4718-b416-40a9bf5444c9)>, <OHLCV: OHLCV obje
ct (2ff52da7-72e1-4a23-895e-1209da5e4863)>, <OHLCV: OHLCV object (84c8a1c3-96de-4da2-90fc-f22cb47c8fca)>, <OHLCV: OH
LCV object (4cf34289-4ca9-41bb-a6c2-b9d86bd28a02)>, <OHLCV: OHLCV object (0c8c4698-2b95-402f-b18b-e96abba7e679)>, <O
HLCV: OHLCV object (9f906cfa-e0ac-44e6-a996-b3c1982d7256)>, <OHLCV: OHLCV object (2e8c2e1b-b2d9-4775-b933-632a25682a
ee)>, <OHLCV: OHLCV object (37e09dbc-3f35-40eb-aa6c-da26d4d272be)>, <OHLCV: OHLCV object (d5fad8bd-bc54-4d15-8ca4-f2
ce74c76025)>, <OHLCV: OHLCV object (bbaf88a8-41e5-44f5-afa0-0956c639b0eb)>, <OHLCV: OHLCV object (50f8459a-1c94-47b3
-817f-b5fbb155f1f4)>, <OHLCV: OHLCV object (6bea514d-2e95-4fa1-b4c2-e782edde8b1b)>, <OHLCV: OHLCV object (37ffe519-1

c70-4431-bdb5-baec34832ca8)>, <OHLCV: OHLCV object (123b3f40-8a52-43f7-9297-f5170a48bc91)>, <OHLCV: OHLCV object (94
ac0309-c741-46cc-8ec2-646dd9db4848)>, <OHLCV: OHLCV object (614eaacf-3797-40e2-9a86-e620fd02ba73)>, '...(remaining e
lements truncated)...']>


In [75]: OHLCV.objects.all()[0].timestamp
Out[75]: datetime.datetime(2019, 3, 13, 9, 30, tzinfo=<UTC>)

In [76]: OHLCV.objects.all().order_by('-timestamp')
Out[76]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/miniconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)

    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/miniconda3/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    400                         if cls is not object \
    401                                 and callable(cls.__dict__.get('__repr__')):
--> 402                             return _repr_pprint(obj, self, cycle)
    403
    404             return _default_pprint(obj, self, cycle)
~/miniconda3/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    695     """A pprint that just redirects to the normal repr function."""
    696     # Find newlines and replace them with p.break_()
--> 697     output = repr(obj)
    698     for idx,output_line in enumerate(output.splitlines()):
    699         if idx:

~/miniconda3/lib/python3.6/site-packages/django/db/models/query.py in __repr__(self)
    242
    243     def __repr__(self):
--> 244         data = list(self[:REPR_OUTPUT_SIZE + 1])

    245         if len(data) > REPR_OUTPUT_SIZE:
    246             data[-1] = "...(remaining elements truncated)..."

~/miniconda3/lib/python3.6/site-packages/django/db/models/query.py in __iter__(self)
    266                - Responsible for turning the rows into model objects.
    267         """
--> 268         self._fetch_all()
    269         return iter(self._result_cache)
    270

~/miniconda3/lib/python3.6/site-packages/django/db/models/query.py in _fetch_all(self)
   1184     def _fetch_all(self):
   1185         if self._result_cache is None:
-> 1186             self._result_cache = list(self._iterable_class(self))
   1187         if self._prefetch_related_lookups and not self._prefetch_done:
   1188             self._prefetch_related_objects()
~/miniconda3/lib/python3.6/site-packages/django/db/models/sql/compiler.py in apply_converters(self, rows, converters
)
   1007                 value = row[pos]
   1008                 for converter in convs:
-> 1009                     value = converter(value, expression, connection)
   1010                 row[pos] = value
   1011             yield row


~/miniconda3/lib/python3.6/site-packages/django/db/backends/sqlite3/operations.py in converter(value, expression, co
nnection)
    252             def converter(value, expression, connection):
    253                 if value is not None:
--> 254                     return create_decimal(value).quantize(quantize_value, context=expression.output_field.co
ntext)
    255         else:
    256             def converter(value, expression, connection):

TypeError: argument must be int of float

In [77]: import django

In [78]: django.__version__
Out[78]: '2.1.5'

型号

class OHLCV(models.Model):
    # General
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    createdAt = models.DateTimeField(auto_now_add=True)
    updatedAt = models.DateTimeField(auto_now=True)

    # OHLCV
    ticker = models.CharField(max_length=20)
    timestamp = models.DateTimeField()
    open = models.DecimalField(max_digits=15, decimal_places=2)
    high = models.DecimalField(max_digits=15, decimal_places=2)
    low = models.DecimalField(max_digits=15, decimal_places=2)
    close = models.DecimalField(max_digits=15, decimal_places=2)
    volume = models.IntegerField(default=0)

    class Meta:
        unique_together = (("ticker", "timestamp",),)

序列化器

class OHLCVSerializer(serializers.ModelSerializer):
    class Meta:
        model = OHLCV
        exclude = ('id', 'createdAt', 'updatedAt',)

查看

class OHLCVView(generics.ListAPIView):
    queryset = OHLCV.objects.all()
    serializer_class = OHLCVSerializer
    permission_classes = (IsAuthenticated,)

    name = 'OHLCV'

    search_fields = (
        '^ticker',
        )
    ordering = (
    'timestamp',
    )

0 个答案:

没有答案
相关问题