How to use Django HStore DictionaryField in schema and schemaless mode at the same time?

python django django-models django-admin django-hstore

220 просмотра

1 ответ

618 Репутация автора

When I use hstore.DictionaryField() in Django model passing no parameters and register my model in Djano admin I can create new key-value rows in admin interface on a fly.

When I use the same field in schema mode hstore.DictionaryField(schema=['some schema description']) I get fixed amount of fields described in schema parameter.

Can I have both of these features at the same time, namely, have several fixed fields of certain type listed in schema description while being able to add new fields as well?

Upd

One way of solving this could be using two DictionaryField's one with schema and other being schemaless but it is hardly an optimal solution.

Автор: Sashko Lykhenko Источник Размещён: 07.07.2016 01:44

Ответы (1)


0 плюса

777 Репутация автора

Решение

The answer is no, you cannot have both with the current implementation of the hstore library. Take a look at the init function (this is from their source on github):

class DictionaryField(HStoreField):
    description = _("A python dictionary in a postgresql hstore field.")

    def __init__(self, *args, **kwargs):
        self.schema = kwargs.pop('schema', None)
        self.schema_mode = False
        # if schema parameter is supplied the behaviour is slightly different
        if self.schema is not None:
            self._validate_schema(self.schema)
            self.schema_mode = True
            # DictionaryField with schema is not editable via admin
            kwargs['editable'] = False
            # null defaults to True to facilitate migrations
            kwargs['null'] = kwargs.get('null', True)

        super(DictionaryField, self).__init__(*args, **kwargs)

You can see that if the schema is set, then the editable argument for the field is set to false, which is what the Django admin looks at to see if you should be allowed to edit the value in the admin UI.

Unfortunately, you only have a few options: override it yourself by creating your own class that inherits from that one, or open up a pull request or issue on their github page and hope that someone will get to it.

Автор: Titus P Размещён: 28.07.2016 05:58
Вопросы из категории :
32x32