class FriendSerializer(serializers.ModelSerializer): foofriend = UserSerializer(read_only=True) barfriend = UserSerializer(read_only=True) class Meta: model = Friend fields = ('foofriend', 'barfriend', 'pk')
class FriendshipSerializer(serializers.ModelSerializer): author = UserSerializer(read_only=True) friend = UserSerializer(read_only=True, source='recipient') class Meta: model = Friendship fields = ('friend', 'author', 'approved', 'pk', 'recipient') read_only_fields = ('approved', ) extra_kwargs = {'recipient': {'write_only': True, 'required': False}}
class FriendshipRequestSerializer(serializers.ModelSerializer): initiator = UserSerializer(read_only=True) recipient = UserSerializer() class Meta: model = FriendRequest fields = ( 'initiator', 'recipient', 'approved', )
class EventSerializer(serializers.HyperlinkedModelSerializer): content_object = GenericRelatedField({ Achieve: AchieveSerializer(read_only=True, allow_null=True), Friendship: FriendshipSerializer(read_only=True, allow_null=True), Post: PostSerializer(read_only=True), }) # content_type = fields.CharField(read_only=True) content_type = fields.SerializerMethodField('get_event_type') created = serializers.DateTimeField(read_only=True, format='%X %d %b %Y') author = UserSerializer() class Meta: model = Event fields = ('id', 'author', 'created', 'title', 'content_object', 'content_type') # exclude = ('content_object',) depth = 0 def get_event_type(self, obj): # print(ContentTypeManager().get_for_model(obj), dir(ContentTypeManager().get_for_model(obj))) content_object_name = str(type(obj.content_object)).replace( '\'>', '').split('.') return content_object_name[len(content_object_name) - 1]
class ChatSerializer(serializers.ModelSerializer): users = UserSerializer(many=True, context={'request': None}) class Meta: fields = ('pk', 'users') model = Chat
class ChatSerializer(serializers.ModelSerializer): author = serializers.ReadOnlyField(source='author.id') subscribers=UserSerializer(many=True) messages = MessageSerializer(many=True, read_only = True) class Meta: model = Chat fields = ('id', 'author', 'title', 'created_time', 'subscribers', 'messages', )
class PostSerializer(serializers.HyperlinkedModelSerializer): likes_count = serializers.IntegerField(source='likes.count', read_only=True) created = serializers.DateTimeField(read_only=True, format='%X %d %b %Y') author = UserSerializer() class Meta: model = Post fields = ('id', 'content', 'author', 'title', 'created', 'likes_count')
class FriendshipSerializer(serializers.ModelSerializer): friend = UserSerializer() content = fields.SerializerMethodField('__null__') class Meta: model = Friendship fields = ['friend', 'content'] def __null__(self): return None
class ChatSerializer(serializers.ModelSerializer): author = UserSerializer(read_only=True) last_message = serializers.SerializerMethodField() def get_last_message(self, obj): try: return MessageSerializer(obj.messages.latest('created')).data except: return None class Meta: model = Chat fields = ['id', 'users', 'last_message', 'author', 'title']
class ChatSerializer(serializers.ModelSerializer): author = UserSerializer(read_only=True) last_message = serializers.SerializerMethodField() def get_last_message(self, obj): # print(obj.messages.all()) try: return MessageSerializer(obj.messages.latest('created')).data except: return None class Meta: model = Chat fields = 'id', 'participants', 'last_message'
class PostSerializer(serializers.HyperlinkedModelSerializer): author = UserSerializer(read_only=True) class Meta: model = Post fields = [ 'content', 'likes_count', 'author', 'created', 'updated', 'id', ] read_only_fields = [ 'likes_count', 'author', 'created', 'updated', 'id', ]
class PostSerializer(serializers.ModelSerializer): author = UserSerializer(read_only=True) class Meta: model = Post fields = 'id', 'text', 'title'
class MessageSerializer(serializers.ModelSerializer): author = UserSerializer(read_only=True) class Meta: model = Message fields = 'id', 'text', 'chat'
class MessageSerializer(serializers.ModelSerializer): author = UserSerializer(read_only=True) class Meta: model = Message fields = ['content', 'chat', 'author', 'created', 'id']