1

The standard approach in admin.py by defining class:

exclude = ('some_hstore_field', )

does not work.

I manage to get expected result by specifying explicit fields but would like rather exclude the one I don't need, than specify all the other needed.

andilabs
  • 22,159
  • 14
  • 114
  • 151

1 Answers1

1

Assuming you have in your model hstore field called facilities:

facilities = hstore.DictionaryField(schema=HSTORE_SCHEMA)

then you CAN NOT just write:

exclude = ('some_non_hstore_field', 'facilities')

Assuming your hstore schema looks something like that:

HSTORE_SCHEMA = [
    {
        "name": "vegetarian_menu",
        "class": "BooleanField",
        "kwargs": {
            "default": False,
            "verbose_name": "vegetarian menu"
        }
    },
    {
        "name": "vegan_menu",
        "class": "BooleanField",
        "kwargs": {
            "default": False,
            "verbose_name": "vegan menu"
        }
    }
]

You have to exclude each of subfield by its name, e.g:

exclude = ('some_non_hstore_field', 'vegetarian_menu', 'vegan_menu')

you can do it like this:

exclude = tuple(['some_non_hstore_field'] + [field['name'] for field in HSTORE_SCHEMA])

or like this - using meta of class field:

exclude = tuple(['some_non_hstore_field'] + [field['name'] for field in YourModel._meta.get_field_by_name('facilities')[0].schema])

The same applies for readonly_fields

andilabs
  • 22,159
  • 14
  • 114
  • 151