Combining DRF unrelated models

2019-08-22 18:51发布

I cant seem to figure this out. I want to sort by user joined time and user's photo time.

views.py

class UserExploreAPIView(generics.ListAPIView):
    """
    User explore API view.
    """
    serializer_class = TimelineSerializer
    permission_classes = (IsAuthenticated,)
    pagination_class = SmallResultsSetPagination

    def get_queryset(self):
        merged_queryset = Places.get_explore_queryset(self.request.user)
        queryset = Places.objects.filter(pk__in=merged_queryset)
        ann_user = User.objects.annotate(photo_time=Extract('date_joined','epoch'))
        return list(chain(ann_user, queryset))

serializers.py

class SomeUserModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = SomeUserModel
        fields = ('photo','id')

class PlacesSerializer(serializers.ModelSerializer, IdFieldMixin):
    photo_link = serializers.CharField(read_only=True)
    user =  UserRelatedSerializer()

    class Meta:
        fields = ['id', 'user']
        model = Places

class TimelineSerializer(serializers.Serializer):     
    photo_time = serializers.CharField(read_only=True)
    places= PlacesSerializer() #this is the error
    someuser= SomeUserModelSerializer()

    class Meta:
        fields = ['photo_time', 'someuser','places']
        ordering = ('photo_time')

When I try to get places it says, User has no attribute places. Well yes. places is a foreignkey to user, user wouldn't have places only the reverse. How do I combine these two models? SomeUserModel works because it has onetoone with user, but it doesn't work with places.

update: added with time

class TimelineItemSerializer(serializers.Serializer):
    time = serializers.CharField(read_only=True)
    places= PlacesSerializer() #this is the error
    someuser= SomeUserModelSerializer()

class TimelineSerializer(serializers.Serializer):     
    time = TimelineItemSerializer()

    class Meta:
        fields = '__all__'
        order_by  = ('time')

update

json ={
time: 1511654400,
{
   modelA: {'atter1, atter2, atter3}
   modelB: {'atter1, atter2, atter3}
},
time:1511136000, 
{
   modelA: {'atter1, atter2, atter3} # could be empty
   modelB: {'atter1, atter2, atter3}
}
}

0条回答
登录 后发表回答