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

时间:2017-02-05 13:08:49

标签: python django serialization

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

serializer.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

views.py

def get_vwap(request):
    """
    List all vwap for a date
    """
    quotes = Quote.objects.filter(date__gt = '2016-05-05')
    serializer = VWAPSerializer(quotes)
    return Response(serializer.data)

models.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 = VWAPSerializer()     打印(再版(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数据作为每个引用的嵌套列表。我希望这是有道理的。我不确定我做错了什么,并希望得到任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要在QuoteSerializer中使用IndicatorSerializer。请参阅django-rest-framework的{​​{3}}。