Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How deserializer data and save it in db? #29

Open
heckad opened this issue Jul 25, 2019 · 1 comment
Open

How deserializer data and save it in db? #29

heckad opened this issue Jul 25, 2019 · 1 comment

Comments

@heckad
Copy link

heckad commented Jul 25, 2019

Store need

{
  'name': 'category 1',
  'children':[
    {'name': 'category 2'},
    {'name': 'category 3'},
  ]
}

I use djangorestframework and trying to write serializer for

class Category(MPTTModel):
    name = models.CharField(max_length=100)

    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['name']

I made it

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ['id', 'name', 'children']
        read_only_fields = ['parents']

    # parent = serializers.ListField()
    children = RecursiveField('CategorySerializer', required=False, allow_null=True, many=True)

And I got the error TypeError: unhashable type: 'collections.OrderedDict'. It seems that the internal fields are not serialized.

@emxxjnm
Copy link

emxxjnm commented Oct 30, 2019

I think combining these two packages (django-mptt, drf-writable-nested) can solve your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants