#28053: Allow fields to specify arbitrary indexes via db_index=Index()
-------------------------------------+-------------------------------------
Reporter: Marc Tamlyn | Owner: Can
| Sarıgöl
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: indexes migrations | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Markus Holtermann):
I had a chat with felixxm discussing the usefulness of this feature.
The initial idea behind this feature, as far as I can reconstruct from
memory, came from the requirement that I wanted to have a custom index on
a field by default, because I didn't want to tell users to add a specific
index to `Meta.indexes`. The naïve approach Marc and I agreed upon back in
the days was about this:
* A field has a `default_index_class` or `default_index`
property/attribute or whatnot. When a field has `db_index=True`, this
index definition would be used.
* Now, a custom field that would want to require an index, could reject
`db_index` as an argument in `__init__` and set it to `True` all the time.
* A user could, however, may would like to be able to make adjustments to
the index that's defined by the field. Thus, passing their own index
definition in place of `db_index` (and not have the field reject the
argument.
This change would also allow to dynamically construct an index based on
the other arguments passed to `__ini__`:
{{{#!python
class MyField(CharField):
def __init__(self, arg1: str, arg2: int, **kwargs):
db_index = kwargs.pop("db_index", None)
if db_index is None:
db_index = MyIndexClass(arg1, math.pi * arg2)
super().__init__(db_index=db_index, **kwargs)
class MyModel(models.Model):
field1 = MyField()
field2 = MyField(db_index=Index(name="foo"))
}}}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28053#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.eeeb436e7e6dbb9e306864ad26ebf4f3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.