title: Django with Postgresql, column must appear in the GROUP BY clause or be used in an aggregate function
body: <p>I'm using Django 1.11 and Postgresql 9.6
  In my app, there is a model called Person, it have several fields. In database, it's a materialized view.</p>
  
  <pre><code>class Person(models.Model):
      personid = models.CharField(max_length=18, primary_key=True)
      count = models.BigIntegerField()
      native = models.CharField(max_length=2)
      ...
  </code></pre>
  
  <p>When execute </p>
  
  <pre><code>persons = Person.objects.values('personid', 'native')\
      .annotate(total=Count('native'))
  </code></pre>
  
  <p>It says <code>psycopg2.ProgrammingError: column "person.native" must appear in the GROUP BY clause or be used in an aggregate function</code></p>
  
  <p>When only <strong>select one column</strong> or <strong>not set the personid as primary key</strong> or <strong>not execute annotate</strong> it won't get error. </p>
  
  <p>I print the query sql:</p>
  
  <pre><code>SELECT
  "person"."native",
  "person"."personid",
  COUNT("person"."native") AS "total"
  FROM "person"
  GROUP BY "person"."native", "person"."personid"
  </code></pre>
  
  <p>What can I do?</p>
  
  <p>I make the view into table and set the personid as primary key, and then no problems.</p>