title: Model Method from rest_framework modelSerializer
body: <p>this is a simple question but I'm very new to django-rest-framework.</p>
  
  <p>I was wondering if there is any way to access a method defined on the model from the serializer.?</p>
  
  <p>Say I have a model</p>
  
  <pre><code>class Listing(models.Model):
      listingid           = models.BigIntegerField(primary_key=True)
      mappingid           = models.BigIntegerField()
      projectlevelid      = models.IntegerField()
      subsellerid         = models.IntegerField()
      iscreatedbyadmin    = models.BooleanField(default=None, null=True)
      createdon           = models.DateTimeField(auto_now_add=True, editable=False)
      validationstatus    = models.SmallIntegerField(default=0)
  
      def is_project(self):
          """ Returns True if listing is of Project Type (projectlevelid=6) else False""" 
          if self.projectlevelid == 6:
              return True
          else:
              return False
  
      def get_project_info(self):
          """Returns False if listing is not mapped to a project, else returns the project info"""
          if self.is_project() == False:
              return False
          return models.Project.objects.get(projectid=self.mappingid)
  </code></pre>
  
  <p><strong>Is it possible for the serializer</strong></p>
  
  <pre><code>class ListingSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = models.MasterListing
  </code></pre>
  
  <p><strong>to have access to <code>Listing.is_project</code> i.e. for an object of the <code>Listing</code> model, can the serializer call its <code>is_project</code> method?</strong></p>
  
  <p>If so, can I set a field in the serializer such that if <code>is_project</code> returns true, the field is populated?
  I am trying for something like this, </p>
  
  <pre><code>class ListingSerializer(serializers.ModelSerializer):
      project = serializers.SomeRELATEDFieldTYPE()    # this field if populated if the `is_project` is true
  
      class Meta:
          model = models.MasterListing
  </code></pre>
  
  <p>I understand I can do this using some combination of <code>required=False</code> and <code>SerializerMethodField</code>, but maybe there is a simpler way?.</p>
  
  <p>Note: It is not possible for me to set a foreign key to the <code>mappingid</code>, since it depends on the <code>projectlevelid</code>. I also can't affect this relationship so no further normalization is possible. I know that there might be some way using content-types, but we are trying to avoid that if it is possible..</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: I solved the problem, but not as the question specified.
  I used this:</p>
  
  <pre><code>class ListingSerializer(serializers.ModelSerializer):
      project = serializers.SerializerMethodField()
  
      def get_project(self, obj):
          """Returns False if listing is not mapped to a project, else returns the project info"""
          if str(obj.projectlevelid) == str(6):
              projectObj = models.Project(projectid=obj.mappingid)
              projectObjSerialized = ProjectSerializer(projectObj)
              return projectObjSerialized.data
          return False
  
      class Meta:
          model = models.MasterListing
  </code></pre>
  
  <p><strong>So, the original question still stands: "Is it possible for the modelSerializer to access its models methods?"</strong>  </p>
  
  <p>Also, another problem that now appears is, <strong>can I make the serializer exclude fields on demand, i.e. can it exclude <code>mappingid</code> and <code>projectlevelid</code> if it is indeed a project?</strong> </p>