Skip to content

Commit 046bd12

Browse files
committed
adding additional django examples
1 parent 855a77f commit 046bd12

14 files changed

+2538
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
title: django.db.models.query BaseIterable Example Code
2+
category: page
3+
slug: django-db-models-query-baseiterable-examples
4+
sortorder: 500011238
5+
toc: False
6+
sidebartitle: django.db.models.query BaseIterable
7+
meta: Python example code for the BaseIterable class from the django.db.models.query module of the Django project.
8+
9+
10+
BaseIterable is a class within the django.db.models.query module of the Django project.
11+
12+
13+
## Example 1 from wagtail
14+
[wagtail](https://github.com/wagtail/wagtail)
15+
([project website](https://wagtail.io/)) is a fantastic
16+
[Django](/django.html)-based CMS with code that is open source
17+
under the
18+
[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
19+
20+
[**wagtail / wagtail / core / query.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/query.py)
21+
22+
```python
23+
# query.py
24+
import posixpath
25+
import warnings
26+
from collections import defaultdict
27+
28+
from django.apps import apps
29+
from django.contrib.contenttypes.models import ContentType
30+
from django.db.models import CharField, Q
31+
from django.db.models.functions import Length, Substr
32+
~~from django.db.models.query import BaseIterable
33+
from treebeard.mp_tree import MP_NodeQuerySet
34+
35+
from wagtail.search.queryset import SearchableQuerySetMixin
36+
37+
38+
class TreeQuerySet(MP_NodeQuerySet):
39+
def delete(self):
40+
super().delete()
41+
42+
delete.queryset_only = True
43+
44+
def descendant_of_q(self, other, inclusive=False):
45+
q = Q(path__startswith=other.path) & Q(depth__gte=other.depth)
46+
47+
if not inclusive:
48+
q &= -Q(pk=other.pk)
49+
50+
return q
51+
52+
def descendant_of(self, other, inclusive=False):
53+
return self.filter(self.descendant_of_q(other, inclusive))
54+
55+
def not_descendant_of(self, other, inclusive=False):
56+
return self.exclude(self.descendant_of_q(other, inclusive))
57+
58+
59+
## ... source file abbreviated to get to BaseIterable examples ...
60+
61+
62+
63+
if missing_pks:
64+
generic_pages = Page.objects.filter(pk__in=missing_pks).select_related('content_type').in_bulk()
65+
warnings.warn(
66+
"Specific versions of the following pages could not be found. "
67+
"This is most likely because a database migration has removed "
68+
"the relevant table or record since the page was created:\n{}".format([
69+
{'id': p.id, 'title': p.title, 'type': p.content_type}
70+
for p in generic_pages.values()
71+
]), category=RuntimeWarning
72+
)
73+
else:
74+
generic_pages = {}
75+
76+
for pk, content_type in pks_and_types:
77+
try:
78+
page = pages_by_type[content_type][pk]
79+
except KeyError:
80+
page = generic_pages[pk]
81+
if annotation_aliases:
82+
for annotation, value in annotations_by_pk.get(page.pk, {}).items():
83+
setattr(page, annotation, value)
84+
yield page
85+
86+
87+
~~class SpecificIterable(BaseIterable):
88+
def __iter__(self):
89+
return specific_iterator(self.queryset)
90+
91+
92+
~~class DeferredSpecificIterable(BaseIterable):
93+
def __iter__(self):
94+
return specific_iterator(self.queryset, defer=True)
95+
96+
97+
98+
## ... source file continues with no further BaseIterable examples...
99+
100+
```
101+
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
title: django.db.models.query EmptyQuerySet Example Code
2+
category: page
3+
slug: django-db-models-query-emptyqueryset-examples
4+
sortorder: 500011239
5+
toc: False
6+
sidebartitle: django.db.models.query EmptyQuerySet
7+
meta: Python example code for the EmptyQuerySet class from the django.db.models.query module of the Django project.
8+
9+
10+
EmptyQuerySet is a class within the django.db.models.query module of the Django project.
11+
12+
13+
## Example 1 from django-wiki
14+
[django-wiki](https://github.com/django-wiki/django-wiki)
15+
([project documentation](https://django-wiki.readthedocs.io/en/master/),
16+
[demo](https://demo.django-wiki.org/),
17+
and [PyPI page](https://pypi.org/project/django-wiki/))
18+
is a wiki system code library for [Django](/django.html)
19+
projects that makes it easier to create user-editable content.
20+
The project aims to provide necessary core features and then
21+
have an easy plugin format for additional features, rather than
22+
having every exhaustive feature built into the core system.
23+
django-wiki is a rewrite of an earlier now-defunct project
24+
named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
25+
26+
The code for django-wiki is provided as open source under the
27+
[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
28+
29+
[**django-wiki / src/wiki / managers.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./managers.py)
30+
31+
```python
32+
# managers.py
33+
from django.db import models
34+
from django.db.models import Count
35+
from django.db.models import Q
36+
~~from django.db.models.query import EmptyQuerySet
37+
from django.db.models.query import QuerySet
38+
from mptt.managers import TreeManager
39+
40+
41+
class ArticleQuerySet(QuerySet):
42+
def can_read(self, user):
43+
if user.has_perm("wiki.moderator"):
44+
return self
45+
if user.is_anonymous:
46+
q = self.filter(other_read=True)
47+
else:
48+
q = self.filter(
49+
Q(other_read=True)
50+
| Q(owner=user)
51+
| (Q(group__user=user) & Q(group_read=True))
52+
).annotate(Count("id"))
53+
return q
54+
55+
def can_write(self, user):
56+
if user.has_perm("wiki.moderator"):
57+
return self
58+
if user.is_anonymous:
59+
q = self.filter(other_write=True)
60+
else:
61+
q = self.filter(
62+
Q(other_write=True)
63+
| Q(owner=user)
64+
| (Q(group__user=user) & Q(group_write=True))
65+
)
66+
return q
67+
68+
def active(self):
69+
return self.filter(current_revision__deleted=False)
70+
71+
72+
~~class ArticleEmptyQuerySet(EmptyQuerySet):
73+
def can_read(self, user):
74+
return self
75+
76+
def can_write(self, user):
77+
return self
78+
79+
def active(self):
80+
return self
81+
82+
83+
class ArticleFkQuerySetMixin:
84+
def can_read(self, user):
85+
if user.has_perm("wiki.moderate"):
86+
return self
87+
if user.is_anonymous:
88+
q = self.filter(article__other_read=True)
89+
else:
90+
q = self.filter(
91+
Q(article__other_read=True)
92+
| Q(article__owner=user)
93+
| (Q(article__group__user=user) & Q(article__group_read=True))
94+
).annotate(Count("id"))
95+
return q
96+
97+
98+
99+
## ... source file abbreviated to get to EmptyQuerySet examples ...
100+
101+
102+
103+
def can_read(self, user):
104+
return self.get_queryset().can_read(user)
105+
106+
def can_write(self, user):
107+
return self.get_queryset().can_write(user)
108+
109+
110+
class ArticleFkManager(models.Manager):
111+
def get_empty_query_set(self):
112+
return self.get_queryset().none()
113+
114+
def get_queryset(self):
115+
return ArticleFkQuerySet(self.model, using=self._db)
116+
117+
def active(self):
118+
return self.get_queryset().active()
119+
120+
def can_read(self, user):
121+
return self.get_queryset().can_read(user)
122+
123+
def can_write(self, user):
124+
return self.get_queryset().can_write(user)
125+
126+
127+
~~class URLPathEmptyQuerySet(EmptyQuerySet, ArticleFkEmptyQuerySetMixin):
128+
def select_related_common(self):
129+
return self
130+
131+
def default_order(self):
132+
return self
133+
134+
135+
class URLPathQuerySet(QuerySet, ArticleFkQuerySetMixin):
136+
def select_related_common(self):
137+
return self.select_related(
138+
"parent", "article__current_revision", "article__owner"
139+
)
140+
141+
def default_order(self):
142+
return self.order_by("article__current_revision__title")
143+
144+
145+
class URLPathManager(TreeManager):
146+
def get_empty_query_set(self):
147+
return self.get_queryset().none()
148+
149+
def get_queryset(self):
150+
return URLPathQuerySet(self.model, using=self._db).order_by(
151+
self.tree_id_attr, self.left_attr
152+
Q(article__other_write=True)
153+
| Q(article__owner=user)
154+
| (Q(article__group__user=user) & Q(article__group_write=True))
155+
).annotate(Count("id"))
156+
return q
157+
158+
def active(self):
159+
return self.filter(article__current_revision__deleted=False)
160+
161+
162+
class ArticleFkEmptyQuerySetMixin:
163+
def can_read(self, user):
164+
return self
165+
166+
def can_write(self, user):
167+
return self
168+
169+
def active(self):
170+
return self
171+
172+
173+
class ArticleFkQuerySet(ArticleFkQuerySetMixin, QuerySet):
174+
pass
175+
176+
177+
~~class ArticleFkEmptyQuerySet(ArticleFkEmptyQuerySetMixin, EmptyQuerySet):
178+
pass
179+
180+
181+
class ArticleManager(models.Manager):
182+
def get_empty_query_set(self):
183+
return self.get_queryset().none()
184+
185+
def get_queryset(self):
186+
return ArticleQuerySet(self.model, using=self._db)
187+
188+
def active(self):
189+
return self.get_queryset().active()
190+
191+
192+
## ... source file continues with no further EmptyQuerySet examples...
193+
194+
```
195+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
title: django.db.models.query ModelIterable Example Code
2+
category: page
3+
slug: django-db-models-query-modeliterable-examples
4+
sortorder: 500011240
5+
toc: False
6+
sidebartitle: django.db.models.query ModelIterable
7+
meta: Python example code for the ModelIterable class from the django.db.models.query module of the Django project.
8+
9+
10+
ModelIterable is a class within the django.db.models.query module of the Django project.
11+
12+
13+
## Example 1 from django-model-utils
14+
[django-model-utils](https://github.com/jazzband/django-model-utils)
15+
([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
16+
and
17+
[PyPI package information](https://pypi.org/project/django-model-utils/))
18+
provides useful mixins and utilities for working with
19+
[Django ORM](/django-orm.html) models in your projects.
20+
21+
The django-model-utils project is open sourced under the
22+
[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
23+
24+
[**django-model-utils / model_utils / managers.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./managers.py)
25+
26+
```python
27+
# managers.py
28+
import django
29+
from django.core.exceptions import ObjectDoesNotExist
30+
from django.db import connection
31+
from django.db import models
32+
from django.db.models.constants import LOOKUP_SEP
33+
from django.db.models.fields.related import OneToOneField, OneToOneRel
34+
~~from django.db.models.query import ModelIterable
35+
from django.db.models.query import QuerySet
36+
from django.db.models.sql.datastructures import Join
37+
38+
39+
~~class InheritanceIterable(ModelIterable):
40+
def __iter__(self):
41+
queryset = self.queryset
42+
~~ iter = ModelIterable(queryset)
43+
if getattr(queryset, 'subclasses', False):
44+
extras = tuple(queryset.query.extra.keys())
45+
subclasses = sorted(queryset.subclasses, key=len, reverse=True)
46+
for obj in iter:
47+
sub_obj = None
48+
for s in subclasses:
49+
sub_obj = queryset._get_sub_obj_recurse(obj, s)
50+
if sub_obj:
51+
break
52+
if not sub_obj:
53+
sub_obj = obj
54+
55+
if getattr(queryset, '_annotated', False):
56+
for k in queryset._annotated:
57+
setattr(sub_obj, k, getattr(obj, k))
58+
59+
for k in extras:
60+
setattr(sub_obj, k, getattr(obj, k))
61+
62+
yield sub_obj
63+
else:
64+
yield from iter
65+
66+
67+
68+
69+
## ... source file continues with no further ModelIterable examples...
70+
71+
```
72+

0 commit comments

Comments
 (0)