django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

Methods generated by `order_with_respect_to` (models)

Open HiPhish opened this issue 3 months ago • 0 comments

Bug report

What's wrong

It is possible to set the order of model instances relative to some other model (such as order of answers to a question in a questionnaire) by specifying order_with_respect_to in a model's Meta class: https://docs.djangoproject.com/en/5.0/ref/models/options/#order-with-respect-to

Doing so generates four new methods in total:

  • On the first model
    • get_next_in_order
    • get_previous_in_order
  • On the other model
    • get_RELATED_order (where RELATED is the name of the first model)
    • set_RELATED_order

However, these methods are not recognized by django-stubs, I get an error as if I had a typo. Making the Meta class inherit from TypedModelMeta has no effect.

How is that should be

These four methods should be recognized, along with the correct type for the primary key. This means the get method returns a QuerySet of whatever type the PK is, and the set method takes an Iterable of whatever the type of the PK is. The next and previous methods return an instance of whatever their model is. They do loop around, so there will always be an object.

System information

  • OS: Void Linux
  • python version: 3.12.2
  • django version: 5.0.3
  • mypy version: 1.9.0
  • django-stubs version: 4.2.7
  • django-stubs-ext version: 4.2.7

HiPhish avatar Mar 31 '24 09:03 HiPhish