This repository has been archived on 2022-09-23. You can view files and clone it, but cannot push or open issues or pull requests.
webdev/website/snippets/serializers.py

18 lines
753 B
Python
Raw Normal View History

2022-01-08 00:40:31 +01:00
from django.contrib.auth.models import User
2022-01-08 00:12:34 +01:00
from rest_framework import serializers
from .models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
2022-01-08 02:26:58 +01:00
class SnippetSerializer(serializers.HyperlinkedModelSerializer):
2022-01-08 00:43:06 +01:00
owner = serializers.ReadOnlyField(source='owner.username')
2022-01-08 02:26:58 +01:00
highlight = serializers.HyperlinkedIdentityField(view_name='snippet-highlight', format='html')
2022-01-08 00:12:34 +01:00
class Meta:
model = Snippet
2022-01-08 02:26:58 +01:00
fields = ['url', 'id', 'highlight', 'owner', 'title', 'code', 'linenos', 'language', 'style']
2022-01-08 00:40:31 +01:00
class UserSerializer(serializers.ModelSerializer):
snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all())
class Meta:
model = User
2022-01-08 02:26:58 +01:00
fields = ['url', 'id', 'username', 'snippets']