Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to implement polymorphic serializer with non-ORM entities?#1034

SafaAlfulaij started this conversation inGeneral
Discussion options

My use case is that I am using aJSONField database column to store 10s types of data in nested arrays and objects.
Array children should have the same type, but that type can have a field related to different serializers.

Example (very simplified and abstracted):

WardrobeSerializer:

  • id:"xxx"
  • type:"wordrobe"
  • relationships:cloths:ClothSerializer(many=True)

ClothSerializer:

  • id:"xxx"
  • type:"cloth"
  • attributes:clothType:"tShirt" or"pant",designer:"Mr. Designer", etc...
  • relationships:details: (polymorphic)TShirtSerializer orPantSerializer

TShirtSerializer:

  • id:"xxx"
  • type:"tShirt"
  • attributes:size:"L",color:"black", etc...

PantSerializer:

  • id:"xxx"
  • type:"pant"
  • attributes:waist:22,pantType:"slim", etc...

Example of data:

[    {"id":"xxx","clothType":"tShirt","designer":"Mr. Designer","details": {"size":"L","color":"black"        }    },    {"id":"xxx","clothType":"pant","designer":"Mr. Designer Number 2","details": {"waist":22,"pantType":"slim"        }    }]
You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

IF the model looks like

classCloth(models.Model):cloth_type=models.JSONField()

Then your serializer could looks like

classClothSerializer(serilizers.ModelSerializer):cloth_type=SerializerMethodField()classMeta:model=Clothdefget_cloth_type(self,obj):# handles cloth type serializing

inget_cloth_type your could call theTShirtSerializer or thePantSerializer.
But this is just one example. There are many ways to rome.

You must be logged in to vote
2 replies
@SafaAlfulaij
Comment options

My main concern is includes and nested relationships.

@jokiefer
Comment options

Hm - ok. Including and nested relationships are done by concrete django models i think. Maybe a maintainer of this project could tell us more about that.

I would recommend you to modelling this on the django db layer like this:

classCloth(PolymorphicModel):designer=models.CharField(max_length=256)classTShirt(Cloth):size=models.CharField(max_length=50,choices=["S","M","L", ])color=models.CharField(max_length=50,choices=["black","white"])classPath(Cloth):waist=models.CharField(max_length=50,choices=["22","23","24", ])pant_type=models.CharField(max_length=50,choices=["slim","regular"])

Then the only thing you need to do then is to write aPolymorphic serializer.

I don't know anything about your constraints and why you want to use JSONFields instead of modelling concrete models and relations on the database. But in my experience with django in most cases it is better to modelling a concrete model instead of trying to implement generic things and put it in flat database fields. This is cause any site-package you will use is based on the django model concept (also this package too). I think you could save time if you first rethink about your django model layer instead of trying to hardly use site-packages for your solution if this is not really necessary. Your code base would also become more maintainable ;-)

But this is just my opinion :-) feel free to implement a solution which matches your use cases.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@SafaAlfulaij@jokiefer

[8]ページ先頭

©2009-2025 Movatter.jp