Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize APIRequestLog #25

Open
seth12 opened this issue Aug 15, 2020 · 2 comments
Open

Serialize APIRequestLog #25

seth12 opened this issue Aug 15, 2020 · 2 comments

Comments

@seth12
Copy link

seth12 commented Aug 15, 2020

It is possible to serialize APIRequestLog when i try to create a serialize only user.id is returning:
Here is my class:
class ApiRequestTableSerializer(serializers.Serializer):
requested_at = serializers.DateTimeField(format='%d %b %Y')
path = serializers.CharField(source="get_path", read_only=True)

class Meta:
    model = APIRequestLog
    fields = ['id', 'requested_at', 'response_ms', 'status_code', 'method','user'
              'path', 'remote_addr', 'host',
              'query_params']
@eshaan7
Copy link

eshaan7 commented Apr 23, 2021

Your code but formatted:

class ApiRequestTableSerializer(serializers.Serializer):
   requested_at = serializers.DateTimeField(format='%d %b %Y')
   path = serializers.CharField(source="get_path", read_only=True)

   class Meta:
       model = APIRequestLog
       fields = ['id', 'requested_at', 'response_ms', 'status_code', 'method', 'user',
              'path', 'remote_addr', 'host',
              'query_params']

@basith-rahman
Copy link

basith-rahman commented Apr 13, 2022

Use serializers.ModelSerializer instead of serializers.Serializer.

Import serializers and APIRequestLog model.

from rest_framework import serializers
from rest_framework_tracking.models import APIRequestLog

Then:-

class ApiRequestTableSerializer(serializers.ModelSerializer):
   requested_at = serializers.DateTimeField(format='%d %b %Y')
   path = serializers.CharField(source="get_path", read_only=True)

   class Meta:
       model = APIRequestLog
       fields = ['id', 'requested_at', 'response_ms', 'status_code', 'method', 'user',
              'path', 'remote_addr', 'host',
              'query_params']

It is working for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants