Django序列化程序未返回所有字段

2024-09-30 12:16:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我的序列化程序没有返回模型中的所有字段。只是嵌套的序列化程序。在

序列化程序.py

class IndicatorSerializer(serializers.ModelSerializer):       
    class Meta:
        model = Indicator

class QuoteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Quote

class VWAPSerializer(serializers.ModelSerializer):
    vwap = serializers.SerializerMethodField()

    class Meta:
        model = Quote
        fields = ('date','open','high','low','close','vwap',)

    def get_vwap(self, obj):
        indicators = Indicator.objects.filter(quote__in = obj)
        return IndicatorSerializer(indicators,many=True).data

视图.py

^{pr2}$

模型.py

class Quote(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now_add=True)
    symbol = models.ForeignKey(Symbol, on_delete=models.CASCADE)
    date = models.DateField(blank=True, null=True)
    open = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    high = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    low = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    close = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    last = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    prevclose = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    tottrdqty = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    tottrdval = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    total_trades = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)

    class Meta:
        index_together = [
                ["symbol", "date"],
            ]

class Indicator(models.Model):
    quote = models.ForeignKey(Quote, on_delete=models.CASCADE)
    indicator = models.ForeignKey(IndicatorDefinition, on_delete=models.CASCADE, db_index=True)
    value = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)

    class Meta:
        unique_together = ('quote', 'indicator')

回应

"vwap": [
        {
            "id": 311743,
            "value": "1188.98",
            "quote": 77437,
            "indicator": 1
        },
        {
            "id": 311742,
            "value": "1185.52",
            "quote": 77437,
            "indicator": 2
        },

输出 ser=VwasSerializer() 打印(repr(ser))

VWAPSerializer():
    date = DateField(allow_null=True, required=False)
    open = DecimalField(allow_null=True, decimal_places=2, max_digits=15, required=False)
    high = DecimalField(allow_null=True, decimal_places=2, max_digits=15, required=False)
    low = DecimalField(allow_null=True, decimal_places=2, max_digits=15, required=False)
    close = DecimalField(allow_null=True, decimal_places=2, max_digits=15, required=False)
    vwap = SerializerMethodField()

我不知道这是否有助于我在日志中看到这一点

VariableDoesNotExist: Failed lookup for key [name] in u'<RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>'
[DEBUG 2017-02-05 18:59:47,163] base.py [:929] _resolve_lookup: Exception while resolving variable 'name' in template 'unknown'.
Traceback (most recent call last):
  File "/Users/avgeorge/trader/python/amrapali/venv/lib/python2.7/site-packages/django/template/base.py", line 907, in _resolve_lookup
    (bit, current))  # missing attribute
VariableDoesNotExist: Failed lookup for key [name] in u'<RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>

我要找的是一组给定的报价,以返回所有报价信息(根据模型),以及作为每个报价的嵌套列表的VWAP数据。我希望这有道理。我不知道我做错了什么,希望你能帮我。在


Tags: pytrueadminmodelsnullmaxmetaclass

热门问题