django-commons / drf-excel

An XLSX spreadsheet renderer for Django REST Framework.
BSD 3-Clause "New" or "Revised" License
217 stars 40 forks source link

NESTED: using a nested GET api/serializer writes only headers, fields are empty - fix in PR #36 #37

Closed ionescu77 closed 3 years ago

ionescu77 commented 3 years ago

there is already a fix, a PR (#36) opened by @paveloder

I have applied the fix locally and I can confirm it works.

My API was returning:

HTTP 200 OK
Allow: GET
Content-Type: application/json
Vary: Accept

[
    {
        "id": 1,
        "rfid_tag": {
            "id": 1,
            "number": "1234567890",
            "product": {
                "id": 1,
                "name": "Prosop mare",
                "ean": "11111",
                "description": "",
                "category": 1,
                "info": []
            },
            "status": "I",
            "info": []
        },
        "security_location": 1,
        "aknowledged": false
    },
    {
        "id": 2,
        "rfid_tag": {
            "id": 2,
            "number": "1234567891",
            "product": {
                "id": 1,
                "name": "Prosop mare",
                "ean": "11111",
                "description": "",
                "category": 1,
                "info": []
            },
            "status": "I",
            "info": []
        },
        "security_location": 2,
        "aknowledged": false
    }
]
class SecurityEventsGetXLSXViewset(XLSXFileMixin, ReadOnlyModelViewSet):
    """ Generate XLSX File """
    queryset = models.SecurityEvents.objects.all()
    serializer_class = serializers.SecurityEventsGetSerializer
    renderer_classes = (XLSXRenderer,)
    filename = 'SecurityEventsGet_export.xlsx'
Screenshot 2021-06-22 at 15 02 21

After PR #36 patch

Screenshot 2021-06-22 at 15 02 30
FlipperPA commented 3 years ago

@ionescu77 I've merged in pull requests #35 and #36, which created a conflict. I think I've sorted out both properly, but could you please run a test against the latest on the main branch before I push out a release and confirm that it works?

ionescu77 commented 3 years ago

Hello, I will give it a try and report back. Thank you!

ionescu77 commented 3 years ago

Excuse all, I deleted previous comment (it was my problem, I made some changes to serializers.py in my project and it impacted XLSX generation, too).

Test results

So:

# api_views.py
class SecurityEventsGetXLSXViewset(XLSXFileMixin, ReadOnlyModelViewSet):
    """ Generate XLSX File """
    queryset = models.RfidTag.objects.all()
    serializer_class = serializers.RfidTagNestedSerializer
    renderer_classes = (XLSXRenderer,)
    filename = 'RfidTagNested_export.xlsx'
# serializers.py
class RfidTagNestedSerializer(serializers.ModelSerializer):
    product = ProductNestedSerializer()
    class Meta:
        model = models.RfidTag
        fields = ['id', 'number', 'product', 'state', 'info']
[
    {
        "id": 1,
        "number": "111abc",
        "product": {
            "id": 1,
            "name": "default_product",
            "ean": "",
            "description": "default product for foreign/unknown RFID tags",
            "category": {
                "id": 1,
                "name": "default_category",
                "description": "default category for foreign/unknown RFID tags"
            },
            "info": []
        },
        "state": "",
        "info": []
    },
    {
        "id": 2,
        "number": "111ABC",
        "product": {
            "id": 1,
            "name": "default_product",
            "ean": "",
            "description": "default product for foreign/unknown RFID tags",
            "category": {
                "id": 1,
                "name": "default_category",
                "description": "default category for foreign/unknown RFID tags"
            },
            "info": []
        },
        "state": "",
        "info": []
    },
    {
        "id": 3,
        "number": "111ABCD",
        "product": {
            "id": 1,
            "name": "default_product",
            "ean": "",
            "description": "default product for foreign/unknown RFID tags",
            "category": {
                "id": 1,
                "name": "default_category",
                "description": "default category for foreign/unknown RFID tags"
            },
            "info": []
        },
        "state": "",
        "info": []
    },
    {
        "id": 4,
        "number": "111ABCDE",
        "product": {
            "id": 3,
            "name": "Prosop mic",
            "ean": "222",
            "description": "",
            "category": {
                "id": 2,
                "name": "Textile Camera",
                "description": ""
            },
            "info": []
        },
        "state": "I",
        "info": []
    },
    {
        "id": 5,
        "number": "111ABCDEF",
        "product": {
            "id": 2,
            "name": "Prosop mare",
            "ean": "111",
            "description": "",
            "category": {
                "id": 2,
                "name": "Textile Camera",
                "description": ""
            },
            "info": []
        },
        "state": "I",
        "info": []
    },
    {
        "id": 6,
        "number": "string",
        "product": {
            "id": 1,
            "name": "default_product",
            "ean": "",
            "description": "default product for foreign/unknown RFID tags",
            "category": {
                "id": 1,
                "name": "default_category",
                "description": "default category for foreign/unknown RFID tags"
            },
            "info": []
        },
        "state": "",
        "info": []
    },
    {
        "id": 7,
        "number": "abc",
        "product": {
            "id": 1,
            "name": "default_product",
            "ean": "",
            "description": "default product for foreign/unknown RFID tags",
            "category": {
                "id": 1,
                "name": "default_category",
                "description": "default category for foreign/unknown RFID tags"
            },
            "info": []
        },
        "state": "",
        "info": []
    },
    {
        "id": 8,
        "number": "3034802c801c4ad6a5a4372e",
        "product": {
            "id": 1,
            "name": "default_product",
            "ean": "",
            "description": "default product for foreign/unknown RFID tags",
            "category": {
                "id": 1,
                "name": "default_category",
                "description": "default category for foreign/unknown RFID tags"
            },
            "info": []
        },
        "state": "",
        "info": []
    }
]

And the XLSX:

Screenshot 2021-07-06 at 17 00 26
FlipperPA commented 3 years ago

The fix has been released in version 0.4.1: https://pypi.org/project/drf-renderer-xlsx/0.4.1/