如何获取嵌套的序列化程序以在父视图中显示所有字段

时间:2019-05-14 17:53:00

标签: django rest django-rest-framework

我在两个序列化器之间有嵌套关系。出于某种原因,子序列化器不会显示其所有数据。我只看到一个引用序列化程序工作的类的类型和一个ID。

我尝试查找此内容,并看到了许多编码这种关系的方法。我所发现的一切都使我相信我应该做的事情。我花了一个星期的时间研究这个问题,但我完全陷入了困境。

编辑以获取更多信息: 我遵循此: https://www.django-rest-framework.org/api-guide/relations/#nested-relationships 并按照他们的文档说的方式进行设置(据我所知)他们的结果是我期望得到的更多。这是直接在NESTED RELATIONSHIPS标头下的结果。 / EDIT

序列化器


class QuotelineSerializer(serializers.ModelSerializer):
    # info = QuoteinfoSerializer(many=True, read_only=True)
    class Meta:
        model = Quoteline
        fields = (
            'lineid',
            'quoteid',
            'linenumber',
            'lineitem',
            'enabled',
            ...
            'pricebookprice',
        )


class QuoteinfoSerializer(serializers.ModelSerializer):
    lines = QuotelineSerializer(many= True)

    class Meta:
        model = Quoteinfo
        fields = (
            'quoteid',
            'quotenumber',
            'quotecustomer',
            'quotecsr',
            'quoteuser',
            ...
             'lines'
            )


模型

class Quoteinfo(models.Model):
    quoteid = models.AutoField(db_column='QuoteID', primary_key=True) 
    quotenumber = models.CharField(db_column='QuoteNumber', max_length=30)
    quotecustomer = models.CharField(db_column='QuoteCustomer', max_length=13)
    quotecsr = models.CharField(db_column='QuoteCSR', max_length=25, blank=True, null=True)
    salesrep = models.CharField(db_column='SalesRep', max_length=3, blank=True, null=True)
...
 quoted = models.CharField(db_column='Quoted', max_length=30, blank=True, null=True) 
    market = models.CharField(db_column='Market', max_length=25, blank=True, null=True)

    def __str__(self):
        return self.quotenumber

    class Meta:
        managed = False
        db_table = 'QuoteInfo'



class Quoteline(models.Model):
    # head = models.ForeignKey(Quoteinfo, related_name='lines', on_delete=models.CASCADE)
    lineid = models.AutoField(db_column='LineID', primary_key=True)
    quoteid = models.ForeignKey(Quoteinfo, related_name='lines', db_column='QuoteID', on_delete=models.CASCADE)
    linenumber = models.IntegerField(db_column='LineNumber', blank=True, null=True)
...
    pricebookprice = models.DecimalField(db_column='PriceBookPrice', max_digits=19, decimal_places=4, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'QuoteLine'

我希望我能得到

 "data": [
        {
            "type": "quotes",
            "id": "156711",
            "attributes": {
                "quoteid": 156711,
                "quotenumber": "022819-513240-1X"
                ... more fields matching model + serializer
                 "market": "K-12                     "
            },
            "relationships": {
                "lines": {
                    "data": [
                        {
                            "type": "Quoteline",
                            "id": "596214"
                            ^^^^^^^^ This is all i get
                            THIS IS WHERE MY PROBLEM LIES
                            I expect to see 
                            "attributes":{
                            A bunch of fields matching models here

                            }
                            ^^^^^^^^^ this is what I want that is missing
                        },

0 个答案:

没有答案