- Notifications
You must be signed in to change notification settings - Fork302
Including GenericForeignKey fields#942
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
When you have a model defined as: classTaggedItem(models.Model):tag=models.SlugField()content_type=models.ForeignKey(ContentType,on_delete=models.CASCADE)object_id=models.PositiveIntegerField()content_object=GenericForeignKey('content_type','object_id') And a serializer defined as: classTaggedItemSerializer(serializers.ModelSerializer):content_object=relations.ResourceRelatedField(read_only=True)# This returns correct results based on the object (proper `id`s and `type`s)included_serializers= {"content_object":"",# What to have here? }classMeta:model=TaggedItemfields= ("tag","content_object") How to use the include "feature" for generic foreign keys? |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 2 comments 4 replies
-
This is achievable by using PolymorphicResourceRelatedField, with an abstract class being the base class (this fails in schema generation in DRF side as abstract classes shouldn't be used in |
BetaWas this translation helpful?Give feedback.
All reactions
-
I've found a related discussion onJSON:API forums which discusses polymorphic relationships and its challenges. Especially when it comes to |
BetaWas this translation helpful?Give feedback.
All reactions
-
As mentioned, |
BetaWas this translation helpful?Give feedback.
All reactions
-
In the example app there is an example which uses the reverse relationship using Like the following: classTaggedItem(BaseModel):tag=models.SlugField()content_type=models.ForeignKey(ContentType,on_delete=models.CASCADE)object_id=models.PositiveIntegerField()content_object=GenericForeignKey("content_type","object_id")def__str__(self):returnself.tagclassMeta:ordering= ("id",)classBlog(BaseModel):name=models.CharField(max_length=100)tagline=models.TextField()tags=GenericRelation(TaggedItem)def__str__(self):returnself.nameclassMeta:ordering= ("id",)classTaggedItemSerializer(serializers.ModelSerializer):classMeta:model=TaggedItemfields= ("tag",)classBlogSerializer(serializers.ModelSerializer):copyright=serializers.SerializerMethodField()tags=relations.ResourceRelatedField(many=True,read_only=True)included_serializers= {"tags":"example.serializers.TaggedItemSerializer", }defget_copyright(self,resource):returndatetime.now().yeardefget_root_meta(self,resource,many):return {"api_docs":"/docs/api/blogs"}classMeta:model=Blogfields= ("name","url","tags")read_only_fields= ("tags",)meta_fields= ("copyright",) To directly serialize |
BetaWas this translation helpful?Give feedback.
All reactions
-
as far as I can remember, django core devs suggest not to use genericforeignkey |
BetaWas this translation helpful?Give feedback.
All reactions
-
Can you please share the source? It's not the best, I know, but sometimes it's a must... |
BetaWas this translation helpful?Give feedback.