djangonauts / django-hstore

PostgreSQL HStore support for Django.
http://django-hstore.readthedocs.io/
Other
517 stars 142 forks source link

Schema mode and custom fields #94

Closed honi closed 9 years ago

honi commented 9 years ago

I'm trying to use schema mode with a custom field, but no luck so far.

I'm stripping down the code to make a simple example to post here. Is it supposed to work if schema contains "class" key pointing to a model field class?

nemesifier commented 9 years ago

is your custom field a text/number field, or is it a more complex thing like a foreignkey or others?

honi commented 9 years ago

I figured that since hstore only accepts strings, the custom field must coerce whatever value it uses to a string in order to work in django-hstore.

Anyways, yes, the custom field is a subclass of CharField.

nemesifier commented 9 years ago

If there's an issue with the code, please send a bug report. For other questions use the mailing list.

mathieubellon commented 9 years ago

We are struggling on this exact same issue. We subclass models.Field with not luck ATM.

Can't figure out if this is a bug. Where should I discuss this ? (I already opened a thread on Google group)

mathieubellon commented 9 years ago

Hi @honi could you please provide more insight on the coercition your are talking about ?

nemesifier commented 9 years ago

@matthieubellon, unfortunately time is tirant, so sometimes messages can pass unanswered on the ML. Try to bump the message on the ML and give more info if you have them.

honi commented 9 years ago

@matthieubellon, I was writing a custom field that uses a multiple choice checkbox widget, and saves data in the DB as CSV. I stumbled upon this issue when my choices had integer values.

Even if I could always coerce the choice values to strs when saving, I didn't have an easy way to know that I needed to cast them back to something other than str when constructing the field/widget. This in turn gave me trouble when trying to display the current value in the widget or when cleaning the widget's value.

In the end, I still used the custom field with the multiple choice checkbox widget, but I enforced that all choice values were strs. Then I added a simple method in the model to get the display value for a given choice (this method read the schema and finds the choice by the given value, and return's it's label).

Feel free to link to the ML or something if this is getting offtopic here.