Ticket #24858: get_FIELD_display.patch

File get_FIELD_display.patch, 2.5 KB (added by chedi, 9 years ago)

get_FIELD_display fix for the ArrayField field

  • django/contrib/postgres/fields/array.py

    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  
    11import json
     2import collections
    23
    34from django.contrib.postgres import lookups
    45from django.contrib.postgres.forms import SimpleArrayField
    class ArrayField(Field):  
    160161        defaults.update(kwargs)
    161162        return super(ArrayField, self).formfield(**defaults)
    162163
     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
    163185
    164186@ArrayField.register_lookup
    165187class ArrayContains(lookups.DataContains):
  • django/db/models/base.py

    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  
    33import copy
    44import inspect
    55import warnings
     6import collections
    67from itertools import chain
    78
    89from django.apps import apps
    class Model(six.with_metaclass(ModelBase)):  
    839840
    840841    def _get_FIELD_display(self, field):
    841842        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')
    842848        return force_text(dict(field.flatchoices).get(value, value), strings_only=True)
    843849
    844850    def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
Back to Top