get_FIELD_display support in schema mode

21 views
Skip to first unread message

Szymon Nowicki

unread,
Nov 24, 2015, 5:49:01 AM11/24/15
to django-hstore
I've been using django-hstore in a few projects, and it works really well.
However there is a feature I would like to add to it.

Django has a very useful feature which adds a get_FIELD_display() function for each field with choices.
At the moment I subclass DictionaryField to add this feature, but this might be useful for others.

All that needs to be done is this code has to be added at the end of _create_hstore_virtual_fields()
in DictionaryField:

if virtual_field.choices:
    setattr
(cls, 'get_%s_display' % field['name'],
        curry
(cls._get_FIELD_display, field=virtual_field))


and something similar in _remove_hstore_virtual_fields().

I could create a pull request with tests if you think this idea is ok.

nemesis

unread,
Dec 3, 2015, 11:05:17 AM12/3/15
to django-hstore
Hi Szymon,

sorry for my late response.

It sounds great, although I don't know right now how much effort it requires.

Have you taken a look at it?

Federico

Szymon Nowicki

unread,
Dec 4, 2015, 4:24:23 AM12/4/15
to django-hstore
Yes, I have actually implemented a simplified version of it in one of my projects.
It seems fairly straightforward, copying the implementation from the version in django.

I could prepare a pull request over the weekend.

nemesis

unread,
Dec 4, 2015, 12:56:01 PM12/4/15
to django-hstore
For copying do you mean duplicating the code or inheriting it?

Federico

Szymon Nowicki

unread,
Dec 4, 2015, 2:08:17 PM12/4/15
to django...@googlegroups.com
Copying, but apparently I made a mistake.

The functionality I wanted is already there, but it is buggy and does not work.
In https://github.com/djangonauts/django-hstore/blob/master/django_hstore/virtual.py lines 22-24 the required code is there, but on that line self.name is still None, so the created function is always called get_None_display. It should be moved to the end of that function.
I tested it out and it seems to work.

I could fix it and create test cases.
I could also add code to  _remove_hstore_virtual_fields that removes the function after a schema reload.

--
You received this message because you are subscribed to a topic in the Google Groups "django-hstore" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-hstore/7hDLorM5t3o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-hstor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages