mirror of
https://github.com/davidhalter/django-stubs.git
synced 2026-11-18 01:40:50 +08:00
docs(README): Fix indentation in list and examples (#1083)
Run examples through black for uniformity, link `HttpModel`
This commit is contained in:
1 parent
966472994b
commit
5334a80c9b
1 file changed
+17
-2
@@ -120,17 +120,20 @@ Note: This monkey patching approach will only work when using Python 3.7 and hig
|
||||
|
||||
### How can I create a HttpRequest that's guaranteed to have an authenticated user?
|
||||
|
||||
Django's built in `HttpRequest` has the attribute `user` that resolves to the type
|
||||
Django's built in [`HttpRequest`](https://docs.djangoproject.com/en/4.0/ref/request-response/#django.http.HttpRequest) has the attribute `user` that resolves to the type
|
||||
|
||||
```python
|
||||
Union[User, AnonymousUser]
|
||||
```
|
||||
where `User` is the user model specified by the `AUTH_USER_MODEL` setting.
|
||||
|
||||
If you want a `HttpRequest` that you can type-annotate with where you know that the user is authenticated you can subclass the normal `HttpRequest` class like so:
|
||||
|
||||
```python
|
||||
from django.http import HttpRequest
|
||||
from my_user_app.models import MyUser
|
||||
|
||||
|
||||
class AuthenticatedHttpRequest(HttpRequest):
|
||||
user: MyUser
|
||||
```
|
||||
@@ -152,10 +155,12 @@ from django.db import models
|
||||
class MyModelQuerySet(models.QuerySet):
|
||||
pass
|
||||
|
||||
|
||||
class MyModel(models.Model):
|
||||
bar = models.IntegerField()
|
||||
objects = MyModelQuerySet.as_manager()
|
||||
|
||||
|
||||
def use_my_model():
|
||||
foo = MyModel.objects.get(id=1) # This is `Any` but it should be `MyModel`
|
||||
return foo.xyz # No error, but there should be
|
||||
@@ -168,15 +173,19 @@ Example:
|
||||
```python
|
||||
from django.db import models
|
||||
|
||||
|
||||
class MyModelQuerySet(models.QuerySet):
|
||||
pass
|
||||
|
||||
|
||||
MyModelManager = models.Manager.from_queryset(MyModelQuerySet)
|
||||
|
||||
|
||||
class MyModel(models.Model):
|
||||
bar = models.IntegerField()
|
||||
objects = MyModelManager()
|
||||
|
||||
|
||||
def use_my_model():
|
||||
foo = MyModel.objects.get(id=1)
|
||||
return foo.xyz # Gives an error
|
||||
@@ -203,6 +212,7 @@ from django_stubs_ext import WithAnnotations
|
||||
from django.db import models
|
||||
from django.db.models.expressions import Value
|
||||
|
||||
|
||||
class MyModel(models.Model):
|
||||
username = models.CharField(max_length=100)
|
||||
|
||||
@@ -210,17 +220,22 @@ class MyModel(models.Model):
|
||||
def func(m: WithAnnotations[MyModel]) -> str:
|
||||
return m.asdf # OK, since the model is annotated as allowing any attribute
|
||||
|
||||
|
||||
func(MyModel.objects.annotate(foo=Value("")).get(id=1)) # OK
|
||||
func(MyModel.objects.get(id=1)) # Error, since this model will not allow access to any attribute
|
||||
func(
|
||||
MyModel.objects.get(id=1)
|
||||
) # Error, since this model will not allow access to any attribute
|
||||
|
||||
|
||||
class MyTypedDict(TypedDict):
|
||||
foo: str
|
||||
|
||||
|
||||
def func2(m: WithAnnotations[MyModel, MyTypedDict]) -> str:
|
||||
print(m.bar) # Error, since field "bar" is not in MyModel or MyTypedDict.
|
||||
return m.foo # OK, since we said field "foo" was allowed
|
||||
|
||||
|
||||
func(MyModel.objects.annotate(foo=Value("")).get(id=1)) # OK
|
||||
func(MyModel.objects.annotate(bar=Value("")).get(id=1)) # Error
|
||||
```
|
||||
|
||||
Reference in new issue
Block a user