Answer the question
In order to leave comments, you need to log in
Why doesn't django pass id to vue via rest?
A number of functions are tied to id, but I found that this field is constantly undefined in vue. I don’t know if it is transmitted in the json response via api or not? And do I need to specify this field in the serializer?
Here is
a function that gets objects, but django go to it doesn't pass
app.vue:
async getEvents(){
var response = await fetch('http://127.0.0.1:8000/rest/');
this.events = await response.json()
alert(this.events[0].id)
},
def get(self, request):
events = Event.objects.all()
serializer = EventSerializer(events, many=True)
return Response(serializer.data)
class EventSerializer(serializers.Serializer):
title = serializers.CharField()
content = serializers.CharField()
event_date = serializers.DateTimeField()
email = serializers.EmailField()
Answer the question
In order to leave comments, you need to log in
Of course you need to specify the id in the serializer
Also, you have a model, you can use serializers.ModelSerializer like this
class EventSerializer(serializers.ModelSerializer):
"""
Видимо сериализатор мероприятия
"""
class Meta:
model = Event
fields = ('id', 'title', 'content', 'event_date', 'email', )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question