- Notifications
You must be signed in to change notification settings - Fork302
-
When I'm querying a url that use several level of serializers (eg: /knight) I need to specify the included resources in the url otherwise I'm getting an error message I'm made a simple app that you can see below. Thanks for your help. my models.py fromdjango.dbimportmodels# Create your models here.classKnight(models.Model):name=models.CharField(max_length=50)nickname=models.CharField(max_length=50)classStory(models.Model):name=models.CharField(max_length=50)classQuest(models.Model):name=models.CharField(max_length=50)knights=models.ManyToManyField(Knight,related_name='quests')story=models.ForeignKey(Story,on_delete=models.CASCADE,related_name='quest') urls.py fromdjango.urlsimportpath,includefromrest_frameworkimportroutersfromrest_framework_json_apiimportserializersfromrest_framework_json_api.viewsimportModelViewSetfromrest_framework_json_api.relationsimportResourceRelatedFieldfrom .modelsimportKnight,Story,Questfrom .importviewsclassStorySerializer(serializers.ModelSerializer):quest=ResourceRelatedField(queryset=Quest.objects,many=True)included_serializers= {'quest':'knight.urls.QuestSerializer', }classMeta:model=Storyfields= ('id','name','quest')classJSONAPIMeta:included_resources= ['quest']classQuestSerializer(serializers.ModelSerializer):story=ResourceRelatedField(queryset=Story.objects)included_serializers= {'story':'knight.urls.StorySerializer', }classMeta:model=Questfields= ('id','name','story',)classJSONAPIMeta:included_resources= ['story']classKnightSerializer(serializers.ModelSerializer):quests=ResourceRelatedField(queryset=Quest.objects,many=True, )included_serializers= {'quests':'knight.urls.QuestSerializer', }classMeta:model=Knightfields= ('id','name','nickname','quests',)classJSONAPIMeta:included_resources= ['quests']classKnightViewSet(ModelViewSet):queryset=Knight.objects.all()serializer_class=KnightSerializerclassQuestViewSet(ModelViewSet):queryset=Quest.objects.all()serializer_class=QuestSerializerclassStoryViewSet(ModelViewSet):queryset=Story.objects.all()serializer_class=StorySerializerrouter=routers.DefaultRouter()router.register(r'/knights',KnightViewSet)router.register(r'/quests',QuestViewSet)router.register(r'/stories',StoryViewSet)urlpatterns= [path('',include(router.urls)),] |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 2 replies
-
The change#900 in version 4.2.0 most likely causes this issue. The question is though whether there is an error in your setup (before#900 there was no validation done on included resources) or whether there is a regression in DJA itself. When I look at your sample it looks fine to me. Is this a made up example or actual code you use? At there is an example which we use in our test suite which passes. Are you able to reproduce this issue with in the DJA example app? Or if it is easier you could also create a small example app yourself which reproduces the issue and post the repo here. Quick and dirty is fine as long as there is code which I can run on my machine easily to better discover the issue. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
It's prettry close from the code that I'm using but not with "real" data. Here for therepository Thanks again for your time |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thanks for the example repository. It is indeed a regression in DJA. I have create a bug report at#949 |
BetaWas this translation helpful?Give feedback.