title: python django AttributeError: 'QuerySet' object has no attribute 'region'
body: <p>Recently, I am moving an Django project from an old machine with Django 1.4 to a new machine with Django 1.11. The code runs well in the old machine, while I have met the following problem. Any suggestions and hints are very appreciated. </p>
  
  <p>The code is</p>
  
  <p>In views.py </p>
  
  <pre><code>def get(self, request):
      cabinet=Cabinet.objects.all()
      sc = cabinet.serializers.CabinetSerializer(cabinets)
      sc.data
  </code></pre>
  
  <p>Cabinet is a Django Model</p>
  
  <pre><code>class Cabinet(models.Model):
      region = models.ForeignKey(Region)
      lat = models.DecimalField(max_digits=13, decimal_places=10)
      lon = models.DecimalField(max_digits=13, decimal_places=10)
      corridor = models.PositiveIntegerField()
      type = models.CharField(max_length=2)
      milepost = models.DecimalField(max_digits=5, decimal_places=2)
  
      def name(self):
          'Returns the WSDOT name for this cabinet'
          return "%03i%s%05i" % (self.corridor, self.type, self.milepost * 100)
  </code></pre>
  
  <p>CabinetSerializer is a serializer class, which uses rest_framework</p>
  
  <pre><code>from rest_framework import fields
  from rest_components import HyperlinkedModelSerializer
  from models import Cabinet
  class CabinetSerializer(HyperlinkedModelSerializer):
      class Meta:
          model = Cabinet
          fields = ('id', 'region', 'lat', 'lon', 'corridor', 'type', 'milepost', 'url')
  </code></pre>
  
  <p>In rest_components.py</p>
  
  <pre><code>from rest_framework.fields import Field as RF_Field
  from rest_framework.serializers import HyperlinkedModelSerializer as HMS
  class HyperlinkedModelSerializer(HMS):
      'Identical to original, but supports exposing primary key'
      def get_pk_field(self, model_field):
          print("Hello")
          'Returns a RF_Field instance, per BaseSerializer behavior'
          return RF_Field()
  </code></pre>
  
  <p>After I run "sc.data", I got the following error message:
  AttributeError: 'QuerySet' object has no attribute 'region'</p>
  
  <p>However, I check in python </p>
  
  <pre><code>&gt;&gt;&gt;dir(cabinets[0])
  ['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', u'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_check_column_name_clashes', '_check_field_name_clashes', '_check_fields', '_check_id_field', '_check_index_together', '_check_local_fields', '_check_long_column_names', '_check_m2m_through_same_relationship', '_check_managers', '_check_model', '_check_model_name_db_lookup_clashes', '_check_ordering', '_check_swappable', '_check_unique_together', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_save_parents', '_save_table', '_set_pk_val', '_state', 'check', 'clean', 'clean_fields', 'corridor', 'date_error_message', 'delete', 'display_name', 'from_db', 'full_clean', 'get_deferred_fields', 'id', 'lat', 'lon', 'loop_set', 'loopgroup_set', 'loopgroupdescriptor_set', 'milepost', 'name', 'objects', 'pk', 'prepare_database_save', 'refresh_from_db', 'region', 'region_id', 'save', 'save_base', 'serializable_value', 'type', 'unique_error_message', 'validate_unique']
  </code></pre>
  
  <p>region is indeed its attribute. </p>
  
  <p>As suggested by Jerin Peter George: I added many=True
  sc = cabinet.serializers.CabinetSerializer(cabinets,many=True)</p>
  
  <p>However, I still got the following error message</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/srv/virtual_environments/dotfreewaydata/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 742, in data
      ret = super(ListSerializer, self).data
    File "/srv/virtual_environments/dotfreewaydata/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 262, in data
      self._data = self.to_representation(self.instance)
    File "/srv/virtual_environments/dotfreewaydata/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 660, in to_representation
      self.child.to_representation(item) for item in iterable
    File "/srv/virtual_environments/dotfreewaydata/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 504, in to_representation
      ret[field.field_name] = field.to_representation(attribute)
    File "/srv/virtual_environments/dotfreewaydata/local/lib/python2.7/site-packages/rest_framework/relations.py", line 356, in to_representation
      "the serializer." % self.__class__.__name__
  AssertionError: `HyperlinkedRelatedField` requires the request in the serializer context. Add `context={'request': request}` when instantiating the serializer.
  </code></pre>
  
  <p>I appreciate any comments and hints. </p>