title: How to calculate a rank in a property decorator in Django model when the rank is based on another property decorator?
body: <p>I am a new coder and very new to Django/Python.  I am building an application that consolidates results for a rowing race.  I have the following models in models.py.</p>
  
  <pre><code>class Crew(models.Model):
      name = models.CharField(max_length=50)
      id = models.IntegerField(primary_key=True)
      composite_code = models.CharField(max_length=10, blank=True, null=True)
      club = models.ForeignKey(Club, related_name='crews',
      on_delete=models.CASCADE)
      rowing_CRI = models.IntegerField(blank=True, null=True)
      rowing_CRI_max = models.IntegerField(blank=True, null=True)
      sculling_CRI = models.IntegerField(blank=True, null=True)
      sculling_CRI_max = models.IntegerField(blank=True, null=True)
      event = models.ForeignKey(Event, related_name='crews',
      on_delete=models.CASCADE)
      status = models.CharField(max_length=20)
      penalty = models.IntegerField(default=0)
      handicap = models.IntegerField(default=0)
      manual_override_minutes = models.IntegerField(default=0)
      manual_override_seconds = models.IntegerField(default=0)
      manual_override_hundredths_seconds = models.IntegerField(default=0)
      bib_number = models.IntegerField(blank=True, null=True)
      band = models.ForeignKey(Band, related_name='bands',
      on_delete=models.CASCADE, blank=True, null=True)
  
      @property
      def raw_time(self):
          if len(self.times.filter(tap='Start')) &gt; 1 or len(self.times.filter(tap='Finish')) &gt; 1:
              return 0
  
          start = self.times.get(tap='Start').time_tap
          end = self.times.get(tap='Finish').time_tap
          return end - start
  
      @property
      def race_time(self):
          # The race time can include the penalty as by default it is 0
          return self.raw_time + self.penalty*1000
  
      @property
      def event_band(self):
          if not self.band:
              return self.event.name
  
          return str(self.event.name) + ' ' + str(self.band.name)
  
  class RaceTime(models.Model):
      sequence = models.IntegerField()
      bib_number = models.IntegerField(blank=True, null=True,)
      tap = models.CharField(max_length=10)
      time_tap = models.BigIntegerField()
      crew = models.ForeignKey(Crew, related_name='times',
      on_delete=models.SET_NULL, blank=True, null=True,)
  </code></pre>
  
  <p>I want to create two additional properties.  One that will rank a crew by <code>race_time()</code> across all crews and one that will rank crews within their <code>event_band()</code>.</p>
  
  <p>Here is my first attempt to create the overall ranking as a property called rank:</p>
  
  <pre><code>@property
  def rank(self):
      crews = Crew.objects.filter(status__in=('Accepted')).filter(race_time__isnull=False)
      race_times = list(map(lambda crew: crew.race_time, crews)).sort()
      rank = race_times.index(self.race_time) + 1
      return rank
  </code></pre>
  
  <p>This code fails with this error:</p>
  
  <p>Internal Server Error: /api/crews/
  Traceback (most recent call last):
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
      response = get_response(request)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
      response = self.process_exception_by_middleware(e, request)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
      return view_func(*args, **kwargs)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
      return self.dispatch(request, *args, **kwargs)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
      response = self.handle_exception(exc)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
      self.raise_uncaught_exception(exc)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
      raise exc
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
      response = handler(request, *args, **kwargs)
    File "/Users/sianalcock/Development/pairshead-results/results/views.py", line 82, in get
      return Response(serializer.data) # send the JSON to the client
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/serializers.py", line 757, in data
      ret = super().data
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/serializers.py", line 261, in data
      self._data = self.to_representation(self.instance)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/serializers.py", line 675, in to_representation
      self.child.to_representation(item) for item in iterable
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/serializers.py", line 675, in 
      self.child.to_representation(item) for item in iterable
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/serializers.py", line 513, in to_representation
      attribute = field.get_attribute(instance)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/fields.py", line 443, in get_attribute
      return get_attribute(instance, self.source_attrs)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/rest_framework/fields.py", line 93, in get_attribute
      instance = getattr(instance, attr)
    File "/Users/sianalcock/Development/pairshead-results/results/models.py", line 63, in rank
      crews = Crew.objects.filter(status__in='Accepted').filter(race_time__isnull=False)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/db/models/query.py", line 892, in filter
      return self._filter_or_exclude(False, *args, **kwargs)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
      clone.query.add_q(Q(*args, **kwargs))
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1290, in add_q
      clause, _ = self._add_q(q_object, self.used_aliases)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1318, in _add_q
      split_subq=split_subq, simple_col=simple_col,
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1190, in build_filter
      lookups, parts, reffed_expression = self.solve_lookup_type(arg)
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1049, in solve_lookup_type
      _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
    File "/Users/sianalcock/.local/share/virtualenvs/pairshead-results-3ZeqXKPg/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1420, in names_to_path
      "Choices are: %s" % (name, ", ".join(available)))
  django.core.exceptions.FieldError: Cannot resolve keyword 'race_time' into field. Choices are: band, band_id, bib_number, club, club_id, competitors, composite_code, event, event_id, handicap, id, manual_override_hundredths_seconds, manual_override_minutes, manual_override_seconds, name, penalty, raw_time, rowing_CRI, rowing_CRI_max, sculling_CRI, sculling_CRI_max, status, time_only, times
  [30/Sep/2019 13:46:08] "GET /api/crews/ HTTP/1.1" 500 23531</p>
  
  <p>Am I even on the right lines here?</p>