drf - change Foreign key to another field in a serializer using modelviewset

118 views
Skip to first unread message

cyril moreau

unread,
Feb 7, 2019, 1:18:03 PM2/7/19
to Django users
Hi,

I have a ModelViewSet class as :
class EventAPIListCreate( viewsets.ModelViewSet):
 queryset
= EventAPI.objects.all()
 serializer_class
= EventAPISerializer

and my serializer looks like :
class EventAPISerializer(serializers.ModelSerializer):
   class Meta:
        model = EventAPI
       fields = '__all__'

when I hit the rest api endpoint I have the following result :
[
   
{
       
"id": 1,
       
"name": "Cumple",
       
"location": "calle le corbusier",
       
"description": "cest le cumple de cyril",
       
"date": "2019-02-05T01:01:00Z",
       
"participant": 5,
       
"created_at": "2019-02-05T20:43:01.630400Z",
       
"organisator": 2
   
},

   
{
       
"id": 2,
       
"name": "' or 1=1;",
       
"location": "calle le corbusier",
       
"description": "cest le cumple de cyril",
       
"date": "2019-02-05T01:01:00Z",
       
"participant": 4,
       
"created_at": "2019-02-05T20:45:11.265705Z",
       
"organisator": 2
   
},......

In the result you can see that "organisator" is a number because it is a foreign key.

the organisator model is composed as :
ID,
FirstName,
Lastname,
Email


I would like to change the endpoint result to have :
{
       
"id": 1,
       
"name": "Cumple",
       
"location": "calle le corbusier",
       
"description": "cest le cumple de cyril",
       
"date": "2019-02-05T01:01:00Z",
       
"participant": 5,
       
"created_at": "2019-02-05T20:43:01.630400Z",
       
"organisator": "mye...@gmail.com"
   
},....

or
{
       
"id": 1,
       
"name": "Cumple",
       
"location": "calle le corbusier",
       
"description": "cest le cumple de cyril",
       
"date": "2019-02-05T01:01:00Z",
       
"participant": 5,
       
"created_at": "2019-02-05T20:43:01.630400Z",
       
"organisator": "Firstname Lastname"
   
},....


How can I do that?
Can i use customize the serializer or the modelviewset somehow?
should i use an APIView class instead of a modelviewset to be able to get this result?

I would like to keep the modelviewset as i find it very useful, but maybe it is not posible

Thank you

Ing.Daniel Bojorge

unread,
Feb 7, 2019, 2:23:44 PM2/7/19
to django...@googlegroups.com
Hi.

You can customize the queryset. 


Dios L@s Bendiga

Saludos,

 
 

Mi Blog
Nicaragua

"Si ustedes permanecen unidos a mí, y si permanecen fieles a mis enseñanzas, pidan lo que quieran y se les dará.
(Juan 15:7 DHH)
Bendito el varón que se fía en el SEÑOR, y cuya confianza es el SEÑOR.
(Jeremías 17:7 RV2000)



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc44ca0c-442b-4067-92e0-df9dadc28a47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Okware Aldo

unread,
Feb 7, 2019, 7:29:21 PM2/7/19
to django...@googlegroups.com
Hi,

I think you should consider creating a custom serializer, where you can modify that field, though I would advise you create another or modify yo return the json object of the organiser.


Reply all
Reply to author
Forward
0 new messages