Skip to content

Methods generated by order_with_respect_to (models) #2037

Open
@HiPhish

Description

@HiPhish

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestmypy-pluginIssues specific to mypy_django_plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions