djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Issue with serialization

Open night-fury-web opened this issue 8 years ago • 4 comments

First of all, thanks for this nice library.

I have an issue ArrayModelField

I'm integrating DRF with Djongo and here are my models.

class ItemKey(models.Model):
    name = models.CharField(max_length=255)
    type = models.CharField(max_length=10)
    value = models.CharField(max_length=255)
    limit = models.CharField(max_length=10)

    class Meta:
        abstract = True


class ItemKeyForm(forms.ModelForm):
    class Meta:
        model = ItemKey
        fields = (
            'name', 'type', 'value', 'limit'
        )


class CategoryPattern(models.Model):
    # CategoryPattern.objects.create(name='aa', code='bb', main=[ItemKey('aa','bb')], images=[], resources=[])
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=30)
    main = models.ArrayModelField(model_container=ItemKey, model_form_class=ItemKeyForm, null=True)
    images = models.ArrayModelField(model_container=ItemKey, model_form_class=ItemKeyForm, null=True)
    resources = models.ArrayModelField(model_container=ItemKey, model_form_class=ItemKeyForm, null=True)
    objects = models.DjongoManager()

But the API response is

[
    {
        "name": "a1",
        "code": "bb",
        "main": "[<ItemKey: ItemKey object>]",
        "images": "[]",
        "resources": "[]"
    },
    {
        "name": "a1",
        "code": "bb",
        "main": "[<ItemKey: ItemKey object>]",
        "images": "[<ItemKey: ItemKey object>]",
        "resources": "[<ItemKey: ItemKey object>]"
    }
]

And I couldn't use ModelSerializer for ItemKey, since it is abstract class.

Please help me for this issue

night-fury-web avatar Feb 20 '18 19:02 night-fury-web

@sandguardian Since you are embedding ItemKey inside CategoryPattern, do you ever need to serialize ItemKey alone? I'd reckon that upon serializing CategoryPattern, ItemKey gets implicitly serialized.

Having said that, unfortunately I am no expert in DRF. At the moment if this is an issue, you will need look into the DRF internal implementation and figure out what the issue is.

I would like to help you, but will need information on specifically what about Djongo's implementation is causing the issue. Is it only the fact that it's an abstract class? Can you help me out with how the DRF serializer really works.

nesdis avatar Feb 22 '18 02:02 nesdis

@sandguardian, I ran into the same issue. What I did was I manually defined a custom serializer (not ModelSerializer) for my abstract model (by assuming it's just a simple object). I am not sure if this is the best solution, but I can live with it for now. If anyone can provide a better solution, that would be great. For your case, try something like this:

from rest_framework.fields import CharField
from rest_framework.serializers import ModelSerializer, Serializer, SerializerMethodField

class ItemKeySerializer(Serializer):
    name = CharField(max_length=255)
    type = CharField(max_length=10)
    value = CharField(max_length=255)
    limit = CharField(max_length=10)


class CategoryPatternSerializer(ModelSerializer):
    resources = SerializerMethodField()
    
    # Put meta class here

    def get_resources(self, obj):
        resources_serializer = ItemKeySerializer(obj.resources, many=True)
        return resources_serializer.data

renzladia avatar Mar 18 '18 16:03 renzladia

@renzladia If you don't mind me asking, how did you handle adding new items to the ArrayModelField ?

danielgmartins avatar Apr 17 '18 16:04 danielgmartins

Items are added into the ArrayModelField just like any list object

nesdis avatar Aug 20 '18 12:08 nesdis