_get_next_or_previous_by_FIELD populated from Meta
|Reported by:||alexr||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||jdunck@…||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Right now date field and datetime fields use contribute_to_class to add a 'get_next_by_<field name>'. If you look at the code in _get_next_or_previous_by_FIELD, it is pretty general, and should work with any field with a stable sort order.
Maybe instead of creating get_next_by_FIELD and get_previous_by_FIELD based on field type, we could create them based on a Meta attribute in the class. I'm thinking we could call the Meta attribute seekable_fields or something like that.
class Person(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) class Meta: seekable_fields = ('last_name', )
The resulting person objects would have methods named 'get_next_by_last_name' and 'get_previous_by_last_name'.
I think the benefit here is that we are able to specify the fields we want to seek by, instead of only being able to use date fields.
Eventually, we could drop the contribute_to_class methods for the date field, since it is just a special case that could be handled by including the name of any date field you wanted to be able to sort by.
Change History (10)
comment:1 Changed 3 years ago by jdunck
- Cc jdunck@… added
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 3 years ago by russellm
- Triage Stage changed from Unreviewed to Design decision needed