title: Django Rest Framework, Serializer Level Filter
body: <p>I have two models named Account and Story. With the help of a <code>models.Manager</code> on Django, I wrote a method that retrieves the stories posted in the last 24 hours. With Django, this method works very well. The problem is that when I create a serializer with Django Rest Framework it pulls all the stories. To prevent this, I wrote a method called <code>get_image_file</code> and used the method I wrote with the help of <code>models.Manager</code> here, but I could not solve the problem. As a result, how do I pull only the stories that were posted in the last 24 with Django Rest Framework, as I did on the Django side?</p>
  <p>serializers.py</p>
  <pre><code>from rest_framework import serializers
  from ..models import Account, Story
  
  
  class StorySerializer(serializers.ModelSerializer):
      class Meta:
          model = Story
          fields = ['image_file']
  
      def get_image_file(self, obj): 
      #This method doesn't work. DRF pulls all the stories.
          return obj.get_available_stories()
  
  class AccountSerializer(serializers.ModelSerializer):
      
      class Meta:
          model = Account
          fields = '__all__'
  
      
      stories = StorySerializer(many=True)
  </code></pre>
  <p>models.py</p>
  <pre><code>from django.db import models
  import datetime
  
  def upload_to(instance, filename):
      username = instance.account.username
      return '%s/%s/%s' %('stories',username,filename)
  
  def upload_pp(instance, filename):
      return '%s/%s/%s' %('profile_photo', instance.username,filename)
  
  class Account(models.Model):
      username_pk = models.CharField(max_length=122)
      username = models.CharField(max_length=55, unique=True)
      profile_photo_id = models.CharField(max_length=155, unique=True)
      profile_photo = models.ImageField(upload_to=upload_pp, null=True, blank=True)
      profile_photo_width = models.IntegerField(null=True, blank=True)
      profile_photo_height = models.IntegerField(null=True, blank=True)
  
      def __str__(self):
          return self.username
  
  
  class StoryManager(models.Manager):
      def get_available_stories(self, *args, **kwargs):
          date_from = datetime.datetime.now() - datetime.timedelta(days=1)
          return super(StoryManager, self).filter(image_timestamp__gte = date_from)
  
  class Story(models.Model):
      account = models.ForeignKey(to=Account, on_delete=models.CASCADE, related_name=&quot;stories&quot;)
      image_file = models.ImageField(upload_to=upload_to, blank=True, null=True, editable=False)
      image_url = models.URLField()
      image_pk = models.CharField(max_length=60, null=True, blank=True, editable=False, unique=True)
      image_timestamp = models.DateTimeField(null=True, blank=True)
      image_width = models.IntegerField(null=True, blank=True)
      image_height = models.IntegerField(null=True, blank=True)
      objects = StoryManager()
  
      def __str__(self):
          return str(self.account)
  </code></pre>