M
M
marselabdullin2020-07-09 18:55:02
Django
marselabdullin, 2020-07-09 18:55:02

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)
      },

Here I also pass the date of the serializer, in which there is no id field
views.py (api):
def get(self, request):
        events = Event.objects.all()
        serializer = EventSerializer(events, many=True)
        return Response(serializer.data)

Serializer fields
serializers.py:
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

2 answer(s)
N
Nuchimik, 2020-07-10
@marselabdullin

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', )

And in views.py, do not write your get-method, but inherit the class from ListAPIView

V
Vadim, 2020-07-09
@Viji

And if you look through Google Dev Tools?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question