Hello Jan,

I'm a bit surprised the `contribute_to_class` approach isn't working but I 
remember
there was previous discussions to support passing an `Index` instance to 
the `Field.db_index`
option or to allow an `index_class` attribute to be defined on `Field` 
subclasses to
determine what kind of index should be used when passing `db_index=True`.

>From what I remember this feature was planned to be used to replace `
BaseSpatialField`
special handling of `spatial_index` and allow custom fields to define

I'm not sure if there's still plan to support such features but I guess it 
would solve your
problem by exposing a more convenient API.

Cheers,
Simon

Le mardi 5 décembre 2017 14:33:42 UTC-5, Jan Pieter Waagmeester a écrit :
>
> As part of django-modeltrans 
> <https://github.com/zostera/django-modeltrans>, I'm trying to add a 
> GinIndex to the model when a custom Field is added. The simplified version 
> of my naive implementation looks like this:
>
> from django.contrib.postgres.fields import JSONField
> from django.contrib.postgres.indexes import GinIndex
>
> class CustomJSONField(JSONField):
>     def contribute_to_class(self, cls, name):
>         super(CustomJSONField, self).contribute_to_class(cls, name)
>
>         index = GinIndex(fields=[name])
>         index.set_name_with_model(cls)
>         cls._meta.indexes.append(index)
>
>
>
> This works fine when an initial migration is created for the model, but 
> becomes a bit undefined when the custom field is added to an existing model:
>
> 1. When I add the custom field, with class Meta: indexes = [] present (can 
> also be non-empty), the index is added twice (with the same name).
> 2. When I add the custom field with the index, the index is correctly added
> 3. When I remove the index from the custom field, the index is correctly 
> removed.
> 4. When I re-add the index to the custom field, the change is not detected.
>
> When I reported 1 (and 4): https://code.djangoproject.com/ticket/28888, 
> Tim commented:
>
> As there's no documented support for adding indexes in 
>> Field.contribute_to_class(), can you explain why Django is at fault 
>> and/or propose a patch? 
>
>
> Which I understand. I'm open to suggestions for other solutions to add an 
> Index to the model along with the custom field.
>
> But using contribute_to_class seems the cleanest way to do it, and 
> support seems to be almost/halfway there. Is it reasonable to expect this 
> to work? If so, I'm willing to create a patch, but any help/pointers from 
> someone with more in depth knowledge of migrations is appreciated.
>
> Jan Pieter.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/0d580017-d503-433b-89f8-16bf6af6c463%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to