From 77708e1ca1299feb446379fcf6587457a3b25127 Mon Sep 17 00:00:00 2001
From: chedi toueiti <chedi.toueiti@gmail.com>
Date: Fri, 29 May 2015 03:41:28 +0100
Subject: [PATCH] Fix for the ArrayFiel get_FIELD_display() bug
---
django/contrib/postgres/fields/array.py | 22 ++++++++++++++++++++++
django/db/models/base.py | 6 ++++++
2 files changed, 28 insertions(+)
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py
index 9da7ec4..14949a6 100644
a
|
b
|
|
1 | 1 | import json |
| 2 | import collections |
2 | 3 | |
3 | 4 | from django.contrib.postgres import lookups |
4 | 5 | from django.contrib.postgres.forms import SimpleArrayField |
… |
… |
class ArrayField(Field):
|
160 | 161 | defaults.update(kwargs) |
161 | 162 | return super(ArrayField, self).formfield(**defaults) |
162 | 163 | |
| 164 | def _get_flatchoices(self): |
| 165 | flat = [] |
| 166 | for choice, value in self.choices: |
| 167 | if isinstance(value, (list, tuple)): |
| 168 | flat.extend(value) |
| 169 | elif isinstance(choice, collections.Hashable): |
| 170 | flat.append((choice, value)) |
| 171 | elif isinstance(choice, list): |
| 172 | flat.append((tuple(choice), value)) |
| 173 | return flat |
| 174 | |
| 175 | def _get_hashable(self, value): |
| 176 | if isinstance(value, collections.Hashable): |
| 177 | return value |
| 178 | elif isinstance(value, list): |
| 179 | return tuple(value) |
| 180 | else: |
| 181 | raise ValueError('Unexpected unhashable type') |
| 182 | |
| 183 | flatchoices = property(_get_flatchoices) |
| 184 | |
163 | 185 | |
164 | 186 | @ArrayField.register_lookup |
165 | 187 | class ArrayContains(lookups.DataContains): |
diff --git a/django/db/models/base.py b/django/db/models/base.py
index b679d08..610cefc 100644
a
|
b
|
from __future__ import unicode_literals
|
3 | 3 | import copy |
4 | 4 | import inspect |
5 | 5 | import warnings |
| 6 | import collections |
6 | 7 | from itertools import chain |
7 | 8 | |
8 | 9 | from django.apps import apps |
… |
… |
class Model(six.with_metaclass(ModelBase)):
|
839 | 840 | |
840 | 841 | def _get_FIELD_display(self, field): |
841 | 842 | value = getattr(self, field.attname) |
| 843 | if not isinstance(value, collections.Hashable): |
| 844 | try: |
| 845 | value = field._get_hashable(value) |
| 846 | except: |
| 847 | raise ValueError('Value hashing failed') |
842 | 848 | return force_text(dict(field.flatchoices).get(value, value), strings_only=True) |
843 | 849 | |
844 | 850 | def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): |