wapi icon indicating copy to clipboard operation
wapi copied to clipboard

Commit e3f065 Breaks Custom Serializers for Custom QuerySets

Open tdavis opened this issue 15 years ago • 2 comments

One of my models uses a custom QuerySet and Manager. When I return a SerializableResponse of "CustomQuerySet", my object serializer is ignored and instead a response is returned like:

<customqueryset>
    <field1>val1</field1>
    <field2>val2</field2>
    ...
</customqueryset>

This was not the case prior to the latest commit; reverting to the previous commit fixed the issue.

tdavis avatar Jun 04 '09 18:06 tdavis

Could you provide me with a full example? Since there is currently no test suite it's hard to test anything, but what we use.

In my fork, I made modification that allow for more granular control of the output and serialization of errors.

gvangool avatar Jun 07 '09 07:06 gvangool

Sure. I just made this up, but it should give you a good enough idea:

# app/models.py
from django.db import models

class CustomQuerySet(models.query.queryset):
    pass

class CustomManager(models.Manager):
    def get_query_set(self):
        return CustomQuerySet(self.model)

class MyModel(models.Model):
    public_field = models.BooleanField(default=True)
    private_field = models.BooleanField(default=False)
    objects = CustomManager()


# api/models.py (or whatever)
from app.models import MyModel
from wapi import serializers
from wapi.responses import SerializableResponse

class AppApi(object):
    def ns__method(self, request, args):
        return SerializableResponse(MyModel.objects.all())

class MyModelSerializer(serializers.Serializer):
    serializes = MyModel

    @serializers.objname('record')
    def default(self, obj, **kwargs):
        return {
            'public': obj.public_field
        }

# Expected output
"""
...
<record>
    <public>True</public>
</record>
...
"""

# Real output
"""
...
<customqueryset>
    <public_field>True</public_field>
    <private_field>False</private_field>
</customqueryset>
...
"""

I will check out your fork too, thanks.

tdavis avatar Jun 07 '09 21:06 tdavis