Skip to main content

django-hint

Django_hint is a module to help you type hint your django project to work with different IDEs. It has been tested in PyCharm and with pylint in VSCode.

Notice: Python3.6 or later is required for this module

Installation

You can use the pip to install django_hint

pip3 install django_hint

Usage

The following use cases can be type hinted using django_hint to help your IDE recognize the type of variable.

  1. Database QuerySet
  2. Model Class
  3. WSGIRequest
  4. Django-Rest-Framework Token Authentication

As a bonus, all of the native python type hints such as List, Union, Optional etc. from typing module can be imported from django_hint


Database QuerySet

It is used to hint that the variable is an QuerySet object containing multiple objects whose nature we will determine. This type is used for filter functions that return a iterable query, such as filter, all, order_by, etc.
You need to hint it to QueryType and pass the object type inside the []. Example:

from django_hint import QueryType

sample_query: QueryType[SampleModel] = SampleModel.objects.filter(name='sample')

The sample_query variable will be treated as a QuerySet. While looping through the objects, each object will be treated as a SampleModel

Please note that if you extend your model to StandardModelType as explained below, QueryType will not usually be needed


Model Class

Django adds a few attributes to a Model instance which are not available in the models.Model and will not be available in your IDE. The most notable attribute is the Manager which is accessible via an attribute called objects.

To include these attributes in your IDE, You have to extend your model to the StandardModelType class of django_hint as well as models.Model and use it just like any other model class.

StandardModelType provides the objects property and provides the return type of the objects's functions such as filter, get, etc. In order to provide the correct return types, you have to pass the model's name as the generic type to StandardModelType

Note that StandardModeltype will NOT have any effect on your database and will NOT make new migrations on makemigrations command.

from django.db import models
from django_hint import StandardModelType

class SampleModel(models.Model, StandardModelType['SampleModel']):
    """Just like any other model"""
    name: str = models.CharField(max_length=100)



all_samples = SampleModel.objects.all()
for sample in all_samples:
    print(sample.name)

WSGIRequest

It is used to hint the nature of the request argument of the view (both function and class based). The request will be treated as a HttpRequest having the user variable attached to it. Example:

from django_hint import RequestType

def sample_view(request: RequestType):
    if request.user.is_authenticated:
        print(request.POST.get('data'))

Django-Rest-Framework Token Authentication

If you are using the token authentication of the Django-Rest-Framework, the request object will have a user variable and an auth variable of rest_framework.authtoken.models.Token instance. DRFTokenRequestType will hint the IDE of those two variables.

from django_hint import DRFTokenRequestType

def sample_view(request: DRFTokenRequestType):
    print(request.auth.key)

Release files for django-hint 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-hint 0.3.2
File Size Uploaded
django_hint-0.3.2.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-hint 0.3.2
File Interpreter ABI Platform
django_hint-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.7 kB

Release files / django_hint-0.3.2.tar.gz

Download URL django_hint-0.3.2.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
08ca10e13eb647c6dea559714ba095e2e58a34edbc72b866b63ec60af2f2ebd4
BLAKE2b-256 checksum
How to use checksums
afd4098797d432a8c60d9d813fa650d66f7f8f7ae02e0b9e386d2a65f8a2bb2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / django_hint-0.3.2-py3-none-any.whl

Download URL django_hint-0.3.2-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08cc139880787660c8925cfea109804548fa79d91f7f21430a85fddb31bdb12f
BLAKE2b-256 checksum
How to use checksums
137eb6dd73bbc1b9d6fc18e0c0a86867bb4c7464c01ad6f641789902251563fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page