from rest_framework import serializers
from dwitter.models import Comment, Dweet
from dwitter.templatetags.to_gravatar_url import to_gravatar_url
from django.contrib.auth.models import User


class UserSerializer(serializers.ModelSerializer):
    avatar = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')

    def get_avatar(self, obj):
        return to_gravatar_url(obj.email)


class CommentSerializer(serializers.ModelSerializer):
    author = UserSerializer()

    class Meta:
        model = Comment
        fields = (
            'id',
            'text',
            'posted',
            'reply_to',
            'author',
        )


class DweetSerializer(serializers.ModelSerializer):
    id = serializers.ReadOnlyField(source='pk')
    remix_of = serializers.PrimaryKeyRelatedField(
        source='reply_to',
        queryset=Dweet.with_deleted.all()
    )
    awesome_count = serializers.SerializerMethodField()
    has_user_awesomed = serializers.SerializerMethodField()
    author = UserSerializer()
    comments = CommentSerializer(many=True)

    class Meta:
        model = Dweet
        fields = (
            'author',
            'awesome_count',
            'code',
            'comments',
            'id',
            'posted',
            'remix_of',
            'has_user_awesomed',
        )

    def get_has_user_awesomed(self, dweet):
        user = self.context['request'].user
        return dweet.likes.filter(pk=user.pk).exists()

    def get_awesome_count(self, obj):
        return obj.likes.all().count()

@@ -713,21 +713,23 @@
 %0A%0Aclass 
-Dweet
+RemixOf
 Serializ
@@ -769,177 +769,874 @@
     
-id = serializers.ReadOnlyField(source='pk')%0A    remix_of = serializers.PrimaryKeyRelatedField(%0A        source='reply_to',%0A        queryset=Dweet.with_deleted.all()%0A    )
+%22%22%22 Dweet serializer for embedding dweets in the remix_of field %22%22%22%0A%0A    id = serializers.ReadOnlyField(source='pk')%0A%0A    # Don't continue the chain of embedded remixes%0A    # and only include the ID of the next dweet%0A    remix_of = serializers.PrimaryKeyRelatedField(%0A        source='reply_to',%0A        queryset=Dweet.with_deleted.all()%0A    )%0A%0A    awesome_count = serializers.SerializerMethodField()%0A    author = UserSerializer()%0A%0A    class Meta:%0A        model = Dweet%0A        fields = (%0A            'author',%0A            'awesome_count',%0A            'code',%0A            'id',%0A            'posted',%0A            'remix_of',%0A        )%0A%0A    def get_awesome_count(self, obj):%0A        return obj.likes.all().count()%0A%0A%0Aclass DweetSerializer(serializers.ModelSerializer):%0A    id = serializers.ReadOnlyField(source='pk')%0A%0A    remix_of = RemixOfSerializer(source='reply_to')%0A
 %0A   

Change the remix-of field to embed the dweet instead of just pk