jsonfield: Problems withs PostGres 9.3, JSON Field and Query Distinct

As mentionned in #47, using “.distinct()”, which is used by the admin panel creating users but also in several other cases in django processes, triggers the same bug as #47 describes.

A workaround has been found by @mkhattab : The work around for this in the Django admin is to subclass QuerySet and Manager and override the distinct method [to remove the JSON fields from it].

What would be the definite fix ?

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 20 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Solved my case.

Model.some_query_here.order_by('id').distinct('id')

Model.some_query_here.order_by('-views_count', 'id').distinct('views_count', 'id')

Hope, this helps to somebody

I had this issue popping up in Admin list views for models that contained JSON columns (even though I wasn’t showing those field in the view). Interestingly enough, when the view is called, it doesn’t actually call the distinct method on the Manager. My workaround was to subclass the manager, and override the get_query_set method as follows:

def get_queryset(self):
    """
    Returns a new QuerySet object which excludes JSON `key_values` column.
    """
    return QuerySet(self.model, using=self._db).defer('key_values')