def watch_dir(self, path, glob):         path = Path(path)        if not path.is_absolute():            raise ValueError('%s must be absolute.' % path)        try:            path = path.absolute()        except FileNotFoundError:            logger.debug(                'Unable to watch directory %s as it cannot be resolved.',                path,                exc_info=True,            )            return         logger.debug('Watching dir %s with glob %s.', path, glob)         self.directory_globs[path].add(glob) diff --git a/django/utils/translation/reloader.py b/django/utils/translation/reloader.py-- a/django/utils/translation/reloader.py++ b/django/utils/translation/reloader.py


directories.extend(Path(config.path) / 'locale' for config in apps.get_app_configs())         directories.extend(Path(p) for p in settings.LOCALE_PATHS)         for path in directories:            absolute_path = path.absolute()            sender.watch_dir(absolute_path, '**/*.mo')            sender.watch_dir(path, '**/*.mo')   def translation_file_changed(sender, file_path, **kwargs):


Perform the query and return a single object matching the given         keyword arguments.         """        clone = self.filter(*args, **kwargs)        clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)         if self.query.can_filter() and not self.query.distinct_fields:             clone = clone.order_by()         limit = None


Return a new QuerySet instance with the args ANDed to the existing         set.         """<ins>        self._not_support_combined_queries('filter')</ins>         return self._filter_or_exclude(False, *args, **kwargs)      def exclude(self, *args, **kwargs):


Return a new QuerySet instance with NOT (args) ANDed to the existing         set.         """<ins>        self._not_support_combined_queries('exclude')</ins>         return self._filter_or_exclude(True, *args, **kwargs)      def _filter_or_exclude(self, negate, *args, **kwargs):


If select_related(None) is called, clear the list.         """        self._not_support_combined_queries('select_related')         if self._fields is not None:             raise TypeError("Cannot call select_related() after .values() or .values_list()")


When prefetch_related() is called more than once, append to the list of         prefetch lookups. If prefetch_related(None) is called, clear the list.         """<ins>        self._not_support_combined_queries('prefetch_related')</ins>         clone = self._chain()         if lookups == (None,):             clone._prefetch_related_lookups = ()


Return a query set in which the returned objects have been annotated         with extra data or aggregations.         """<ins>        self._not_support_combined_queries('annotate')</ins>         self._validate_values_are_expressions(args + tuple(kwargs.values()), method_name='annotate')         annotations = {}         for arg in args:


def extra(self, select=None, where=None, params=None, tables=None,               order_by=None, select_params=None):         """Add extra SQL fragments to the query."""<ins>        self._not_support_combined_queries('extra')</ins>         assert self.query.can_filter(), \             "Cannot change a query once a slice has been taken"         clone = self._chain()


The only exception to this is if None is passed in as the only         parameter, in which case removal all deferrals.         """<ins>        self._not_support_combined_queries('defer')</ins>         if self._fields is not None:             raise TypeError("Cannot call defer() after .values() or .values_list()")         clone = self._chain()


method and that are not already specified as deferred are loaded         immediately when the queryset is evaluated.         """<ins>        self._not_support_combined_queries('only')</ins>         if self._fields is not None:             raise TypeError("Cannot call only() after .values() or .values_list()")         if fields == (None,):


)             ) <ins>    def _not_support_combined_queries(self, operation_name):        if self.query.combinator:            raise NotSupportedError(                'Calling QuerySet.%s() after %s() is not supported.'                % (operation_name, self.query.combinator)            )</ins>  class InstanceCheckMeta(type):     def __instancecheck__(self, instance):


]         start_response(status, response_headers)         if getattr(response, 'file_to_stream', None) is not None and environ.get('wsgi.file_wrapper'):            response = environ['wsgi.file_wrapper'](response.file_to_stream)            response = environ['wsgi.file_wrapper'](response.file_to_stream, response.block_size)         return response


class Avg(FixDurationInputMixin, NumericOutputFieldMixin, Aggregate):     function = 'AVG'     name = 'Avg'<ins>    allow_distinct = True</ins>   class Count(Aggregate):


class Sum(FixDurationInputMixin, Aggregate):     function = 'SUM'     name = 'Sum'<ins>    allow_distinct = True</ins>   class Variance(NumericOutputFieldMixin, Aggregate):


@deconstructible class F(Combinable):     """An object capable of resolving references to existing query objects."""    # Can the expression be used in a WHERE clause?    filterable = True      def __init__(self, name):         """diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py-- a/django/db/models/sql/query.py++ b/django/db/models/sql/query.py


for v in value:                     self.check_query_object_type(v, opts, field) <ins>    def check_filterable(self, expression):        """Raise an error if expression cannot be used in a WHERE clause."""        if not getattr(expression, 'filterable', 'True'):            raise NotSupportedError(                expression.__class__.__name__ + ' is disallowed in the filter '                'clause.'            )        if hasattr(expression, 'get_source_expressions'):            for expr in expression.get_source_expressions():                self.check_filterable(expr)</ins>     def build_lookup(self, lookups, lhs, rhs):         """         Try to extract transforms and lookup from given lhs.


raise FieldError("Cannot parse keyword query %r" % arg)         lookups, parts, reffed_expression = self.solve_lookup_type(arg)         if not getattr(reffed_expression, 'filterable', True):            raise NotSupportedError(                reffed_expression.__class__.__name__ + ' is disallowed in '                'the filter clause.'            )        self.check_filterable(reffed_expression)          if not allow_joins and len(parts) > 1:             raise FieldError("Joined field references are not permitted in this query")


value = self.resolve_lookup_value(value, can_reuse, allow_joins, simple_col)         used_joins = {k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0)} <ins>        self.check_filterable(value)</ins>         clause = self.where_class()         if reffed_expression:             condition = self.build_lookup(lookups, reffed_expression, value)


from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.ddl_references import IndexColumns<ins>from django.db.backends.utils import strip_quotes</ins>   class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):


if self._field_data_type(old_field) != self._field_data_type(new_field):             self.sql_alter_column_type += ' USING %(column)s::%(type)s'         # Make ALTER TYPE with SERIAL make sense.        table = model._meta.db_table        table = strip_quotes(model._meta.db_table)         if new_type.lower() in ("serial", "bigserial"):            column = new_field.column            column = strip_quotes(new_field.column)             sequence_name = "%s_%s_seq" % (table, column)             col_type = "integer" if new_type.lower() == "serial" else "bigint"             return (diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py-- a/django/db/backends/sqlite3/schema.py++ b/django/db/backends/sqlite3/schema.py


from django.apps.registry import Apps from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.ddl_references import Statement<ins>from django.db.backends.utils import strip_quotes</ins> from django.db.models import UniqueConstraint from django.db.transaction import atomic from django.db.utils import NotSupportedError


body_copy = copy.deepcopy(body)         meta_contents = {             'app_label': model._meta.app_label,            'db_table': 'new__%s' % model._meta.db_table,            'db_table': 'new__%s' % strip_quotes(model._meta.db_table),             'unique_together': unique_together,             'index_together': index_together,             'indexes': indexes,


try:             with self.open(self.manifest_name) as manifest:                 return manifest.read().decode()        except OSError:        except FileNotFoundError:             return None      def load_manifest(self):


from pathlib import Path  from django.conf import settingsfrom django.http import HttpResponse, HttpResponseNotFoundfrom django.http import Http404, HttpResponse, HttpResponseNotFound from django.template import Context, Engine, TemplateDoesNotExist from django.template.defaultfilters import pprintfrom django.urls import Resolver404, resolvefrom django.urls import resolve from django.utils import timezone from django.utils.datastructures import MultiValueDict from django.utils.encoding import force_str


caller = ''     try:         resolver_match = resolve(request.path)    except Resolver404:    except Http404:         pass     else:         obj = resolver_match.func


value = super().get_prep_value(value)         if value is None or isinstance(value, OuterRef):             return value        return int(value)        try:            return int(value)        except (TypeError, ValueError) as e:            raise e.__class__(                "Field '%s' expected a number but got %r." % (self.name, value),            ) from e      def contribute_to_class(self, cls, name, **kwargs):         assert not cls._meta.auto_field, "Model %s can't have more than one AutoField." % cls._meta.label


value = super().get_prep_value(value)         if value is None:             return None        return float(value)        try:            return float(value)        except (TypeError, ValueError) as e:            raise e.__class__(                "Field '%s' expected a number but got %r." % (self.name, value),            ) from e      def get_internal_type(self):         return "FloatField"


value = super().get_prep_value(value)         if value is None:             return None        return int(value)        try:            return int(value)        except (TypeError, ValueError) as e:            raise e.__class__(                "Field '%s' expected a number but got %r." % (self.name, value),            ) from e      def get_internal_type(self):         return "IntegerField"


from itertools import chain  from django.apps import appsfrom django.core.checks import Error, Tags, registerfrom django.conf import settingsfrom django.core.checks import Error, Tags, Warning, register   @register(Tags.models)


indexes[model_index.name].append(model._meta.label)         for model_constraint in model._meta.constraints:             constraints[model_constraint.name].append(model._meta.label)    if settings.DATABASE_ROUTERS:        error_class, error_id = Warning, 'models.W035'        error_hint = (            'You have configured settings.DATABASE_ROUTERS. Verify that %s '            'are correctly routed to separate databases.'        )    else:        error_class, error_id = Error, 'models.E028'        error_hint = None     for db_table, model_labels in db_table_models.items():         if len(model_labels) != 1:            model_labels_str = ', '.join(model_labels)             errors.append(                Error(                error_class(                     "db_table '%s' is used by multiple models: %s."                    % (db_table, ', '.join(db_table_models[db_table])),                    % (db_table, model_labels_str),                     obj=db_table,                    id='models.E028',                    hint=(error_hint % model_labels_str) if error_hint else None,                    id=error_id,                 )             )     for index_name, model_labels in indexes.items():


for key, value in query:         if value is None:             raise TypeError(                'Cannot encode None in a query string. Did you mean to pass '                'an empty string or omit the value?'                "Cannot encode None for key '%s' in a query string. Did you "                "mean to pass an empty string or omit the value?" % key             )         elif not doseq or isinstance(value, (str, bytes)):             query_val = value


for item in itr:                     if item is None:                         raise TypeError(                            'Cannot encode None in a query string. Did you '                            'mean to pass an empty string or omit the value?'                            "Cannot encode None for key '%s' in a query "                            "string. Did you mean to pass an empty string or "                            "omit the value?" % key                         )                     elif not isinstance(item, bytes):                         item = str(item)


def patch_vary_headers(response, newheaders):     """     Add (or update) the "Vary" header in the given HttpResponse object.    newheaders is a list of header names that should be in "Vary". Existing    headers in "Vary" aren't removed.    newheaders is a list of header names that should be in "Vary". If headers    contains an asterisk, then "Vary" header will consist of a single asterisk    '*'. Otherwise, existing headers in "Vary" aren't removed.     """     # Note that we need to keep the original order intact, because cache     # implementations may rely on the order of the Vary contents in, say,


existing_headers = {header.lower() for header in vary_headers}     additional_headers = [newheader for newheader in newheaders                           if newheader.lower() not in existing_headers]    response['Vary'] = ', '.join(vary_headers + additional_headers)    vary_headers += additional_headers    if '*' in vary_headers:        response['Vary'] = '*'    else:        response['Vary'] = ', '.join(vary_headers)   def has_vary_header(response, header_query):


import logging<ins>import warnings</ins> from functools import update_wrapper  from django.core.exceptions import ImproperlyConfigured


from django.template.response import TemplateResponse from django.urls import reverse from django.utils.decorators import classonlymethod<ins>from django.utils.deprecation import RemovedInDjango40Warningfrom django.utils.functional import SimpleLazyObject</ins>  logger = logging.getLogger('django.request')


class TemplateView(TemplateResponseMixin, ContextMixin, View):    """    Render a template. Pass keyword arguments from the URLconf to the context.    """    """Render a template."""     def get(self, request, *args, **kwargs):        context = self.get_context_data(**kwargs)        # RemovedInDjango40Warning: when the deprecation ends, replace with:        #   context = self.get_context_data()        context_kwargs = _wrap_url_kwargs_with_deprecation_warning(kwargs)        context = self.get_context_data(**context_kwargs)         return self.render_to_response(context)  # RemovedInDjango40Warningdef _wrap_url_kwargs_with_deprecation_warning(url_kwargs):    context_kwargs = {}    for key, value in url_kwargs.items():        # Bind into function closure.        @SimpleLazyObject        def access_value(key=key, value=value):            warnings.warn(                'TemplateView passing URL kwargs to the context is '                'deprecated. Reference %s in your template through '                'view.kwargs instead.' % key,                RemovedInDjango40Warning, stacklevel=2,            )            return value        context_kwargs[key] = access_value    return context_kwargs class RedirectView(View):     """Provide a redirect on any GET request."""     permanent = False


return any(child.is_summary for child in self.children)      @staticmethod    def _resolve_rhs(rhs, query, *args, **kwargs):        if hasattr(rhs, 'resolve_expression'):            rhs = rhs.resolve_expression(query, *args, **kwargs)        return rhs    def _resolve_leaf(expr, query, *args, **kwargs):        if hasattr(expr, 'resolve_expression'):            expr = expr.resolve_expression(query, *args, **kwargs)        return expr      @classmethod     def _resolve_node(cls, node, query, *args, **kwargs):         if hasattr(node, 'children'):             for child in node.children:                 cls._resolve_node(child, query, *args, **kwargs)        if hasattr(node, 'lhs'):            node.lhs = cls._resolve_leaf(node.lhs, query, *args, **kwargs)         if hasattr(node, 'rhs'):            node.rhs = cls._resolve_rhs(node.rhs, query, *args, **kwargs)            node.rhs = cls._resolve_leaf(node.rhs, query, *args, **kwargs)      def resolve_expression(self, *args, **kwargs):         clone = self.clone()


DEFAULT_DB_ALIAS, DJANGO_VERSION_PICKLE_KEY, DatabaseError, connection,     connections, router, transaction, )<ins>from django.db.models import NOT_PROVIDED</ins> from django.db.models.constants import LOOKUP_SEP from django.db.models.constraints import CheckConstraint, UniqueConstraint from django.db.models.deletion import CASCADE, Collector


if not pk_set and (force_update or update_fields):             raise ValueError("Cannot force an update in save() with no primary key.")         updated = False<ins>        # Skip an UPDATE when adding an instance and primary key has a default.        if (            not force_insert and            self._state.adding and            self._meta.pk.default and            self._meta.pk.default is not NOT_PROVIDED        ):            force_insert = True</ins>         # If possible, try an UPDATE. If that doesn't update anything, do an INSERT.         if pk_set and not force_insert:             base_qs = cls._base_manager.using(using)


import functools import inspect import re<ins>import string</ins> from importlib import import_module from urllib.parse import quote


For example, 'foo/<int:pk>' returns '^foo\\/(?P<pk>[0-9]+)'     and {'pk': <django.urls.converters.IntConverter>}.     """<ins>    if not set(route).isdisjoint(string.whitespace):        raise ImproperlyConfigured("URL route '%s' cannot contain whitespace." % route)</ins>     original_route = route     parts = ['^']     converters = {}


part_sql = 'SELECT * FROM ({})'.format(part_sql)                     # Add parentheses when combining with compound query if not                     # already added for all compound queries.                    elif not features.supports_slicing_ordering_in_compound:                    elif (                        self.query.subquery or                        not features.supports_slicing_ordering_in_compound                    ):                         part_sql = '({})'.format(part_sql)                 parts += ((part_sql, part_args),)             except EmptyResultSet:


combinator_sql = self.connection.ops.set_operators[combinator]         if all and combinator == 'union':             combinator_sql += ' ALL'        braces = '({})' if features.supports_slicing_ordering_in_compound else '{}'        braces = '{}'        if not self.query.subquery and features.supports_slicing_ordering_in_compound:            braces = '({})'         sql_parts, args_parts = zip(*((braces.format(sql), args) for sql, args in parts))         result = [' {} '.format(combinator_sql).join(sql_parts)]         params = []diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py-- a/django/db/models/sql/query.py++ b/django/db/models/sql/query.py


clone.bump_prefix(query)         clone.subquery = True         clone.where.resolve_expression(query, *args, **kwargs)<ins>        # Resolve combined queries.        if clone.combinator:            clone.combined_queries = tuple([                combined_query.resolve_expression(query, *args, **kwargs)                for combined_query in clone.combined_queries            ])</ins>         for key, value in clone.annotations.items():             resolved = value.resolve_expression(query, *args, **kwargs)             if hasattr(resolved, 'external_aliases'):


attrs.setdefault('class', '')         attrs.update({             'data-ajax--cache': 'true',<ins>            'data-ajax--delay': 250,</ins>             'data-ajax--type': 'GET',             'data-ajax--url': self.get_url(),             'data-theme': 'admin-autocomplete',


from datetime import datetime, timedelta from django import forms from django.conf import settingsfrom django.contrib import messages from django.contrib.admin import FieldListFilter from django.contrib.admin.exceptions import (     DisallowedModelAdminLookup, DisallowedModelAdminToField,


ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR)  class ChangeListSearchForm(forms.Form):    def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        # Populate "fields" dynamically because SEARCH_VAR is a variable:        self.fields = {            SEARCH_VAR: forms.CharField(required=False, strip=False),        } class ChangeList:    search_form_class = ChangeListSearchForm     def __init__(self, request, model, list_display, list_display_links,                  list_filter, date_hierarchy, search_fields, list_select_related,                  list_per_page, list_max_show_all, list_editable, model_admin, sortable_by):


self.sortable_by = sortable_by          # Get search parameters from the query string.<ins>        _search_form = self.search_form_class(request.GET)        if not _search_form.is_valid():            for error in _search_form.errors.values():                messages.error(request, ', '.join(error))        self.query = _search_form.cleaned_data.get(SEARCH_VAR) or ''</ins>         try:             self.page_num = int(request.GET.get(PAGE_VAR, 0))         except ValueError:


self.list_editable = ()         else:             self.list_editable = list_editable<del>        self.query = request.GET.get(SEARCH_VAR, '')</del>         self.queryset = self.get_queryset(request)         self.get_results(request)         if self.is_popup:


self.extra = extra         super().__init__(output_field) <ins>    def __getstate__(self):        state = super().__getstate__()        state.pop('_constructor_args', None)        return state</ins>     def get_source_expressions(self):         return [self.query]


is_popup = context['is_popup']     save_as = context['save_as']     show_save = context.get('show_save', True)    show_save_and_add_another = context.get('show_save_and_add_another', True)     show_save_and_continue = context.get('show_save_and_continue', True)     has_add_permission = context['has_add_permission']     has_change_permission = context['has_change_permission']     has_view_permission = context['has_view_permission']     has_editable_inline_admin_formsets = context['has_editable_inline_admin_formsets']     can_save = (has_change_permission and change) or (has_add_permission and add) or has_editable_inline_admin_formsets    can_save_and_add_another = (        has_add_permission and        not is_popup and        (not save_as or add) and        can_save and        show_save_and_add_another    )     can_save_and_continue = not is_popup and can_save and has_view_permission and show_save_and_continue     can_change = has_change_permission or has_editable_inline_admin_formsets     ctx = Context(context)


change and context.get('show_delete', True)         ),         'show_save_as_new': not is_popup and has_change_permission and change and save_as,        'show_save_and_add_another': (            has_add_permission and not is_popup and            (not save_as or add) and can_save        ),        'show_save_and_add_another': can_save_and_add_another,         'show_save_and_continue': can_save_and_continue,         'show_save': show_save and can_save,         'show_close': not(show_save and can_save)


Find named groups in `pattern` and replace them with the group name. E.g.,     1. ^(?P<a>\w+)/b/(\w+)$ ==> ^<a>/b/(\w+)$     2. ^(?P<a>\w+)/b/(?P<c>\w+)/$ ==> ^<a>/b/<c>/$<ins>    3. ^(?P<a>\w+)/b/(\w+) ==> ^<a>/b/(\w+)    4. ^(?P<a>\w+)/b/(?P<c>\w+) ==> ^<a>/b/<c></ins>     """     named_group_indices = [         (m.start(0), m.end(0), m.group(1))


# Handle nested parentheses, e.g. '^(?P<a>(x|y))/b'.         unmatched_open_brackets, prev_char = 1, None         for idx, val in enumerate(pattern[end:]):<del>            # If brackets are balanced, the end of the string for the current            # named capture group pattern has been reached.            if unmatched_open_brackets == 0:                group_pattern_and_name.append((pattern[start:end + idx], group_name))                break</del>             # Check for unescaped `(` and `)`. They mark the start and end of a             # nested group.             if val == '(' and prev_char != '\\':


elif val == ')' and prev_char != '\\':                 unmatched_open_brackets -= 1             prev_char = val<ins>            # If brackets are balanced, the end of the string for the current            # named capture group pattern has been reached.            if unmatched_open_brackets == 0:                group_pattern_and_name.append((pattern[start:end + idx + 1], group_name))                break</ins>      # Replace the string for named capture groups with their group names.     for group_pattern, group_name in group_pattern_and_name:


Find unnamed groups in `pattern` and replace them with '<var>'. E.g.,     1. ^(?P<a>\w+)/b/(\w+)$ ==> ^(?P<a>\w+)/b/<var>$     2. ^(?P<a>\w+)/b/((x|y)\w+)$ ==> ^(?P<a>\w+)/b/<var>$<ins>    3. ^(?P<a>\w+)/b/(\w+) ==> ^(?P<a>\w+)/b/<var>    4. ^(?P<a>\w+)/b/((x|y)\w+) ==> ^(?P<a>\w+)/b/<var></ins>     """     unnamed_group_indices = [m.start(0) for m in unnamed_group_matcher.finditer(pattern)]     # Indices of the start of unnamed capture groups.


# Handle nested parentheses, e.g. '^b/((x|y)\w+)$'.         unmatched_open_brackets, prev_char = 1, None         for idx, val in enumerate(pattern[start + 1:]):<del>            if unmatched_open_brackets == 0:                group_indices.append((start, start + 1 + idx))                break</del>             # Check for unescaped `(` and `)`. They mark the start and end of             # a nested group.             if val == '(' and prev_char != '\\':


unmatched_open_brackets -= 1             prev_char = val <ins>            if unmatched_open_brackets == 0:                group_indices.append((start, start + 2 + idx))                break</ins>     # Remove unnamed group matches inside other unnamed capture groups.     group_start_end_indices = []     prev_end = None


value = connection.ops.validate_autopk_value(value)         return value     def get_prep_value(self, value):        from django.db.models.expressions import OuterRef        return value if isinstance(value, OuterRef) else super().get_prep_value(value)     def contribute_to_class(self, cls, name, **kwargs):         assert not cls._meta.auto_field, (             "Model %s can't have more than one auto-generated field."diff --git a/django/db/models/fields/related_lookups.py b/django/db/models/fields/related_lookups.py-- a/django/db/models/fields/related_lookups.py++ b/django/db/models/fields/related_lookups.py


class RelatedLookupMixin:     def get_prep_lookup(self):        if not isinstance(self.lhs, MultiColSource) and self.rhs_is_direct_value():        if not isinstance(self.lhs, MultiColSource) and not hasattr(self.rhs, 'resolve_expression'):             # If we get here, we are dealing with single-column relations.             self.rhs = get_normalized_value(self.rhs, self.lhs)[0]             # We need to run the related field's get_prep_value(). Consider casediff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py-- a/django/db/models/sql/query.py++ b/django/db/models/sql/query.py


handle.         """         filter_lhs, filter_rhs = filter_expr        if isinstance(filter_rhs, F):        if isinstance(filter_rhs, OuterRef):            filter_expr = (filter_lhs, OuterRef(filter_rhs))        elif isinstance(filter_rhs, F):             filter_expr = (filter_lhs, OuterRef(filter_rhs.name))         # Generate the inner query.         query = Query(self.model)


old_field_name = self.renamed_fields.get((app_label, model_name, field_name), field_name)             old_field = self.old_apps.get_model(app_label, old_model_name)._meta.get_field(old_field_name)             new_field = self.new_apps.get_model(app_label, model_name)._meta.get_field(field_name)<ins>            dependencies = []</ins>             # Implement any model renames on relations; these are handled by RenameModel             # so we need to exclude them from the comparison             if hasattr(new_field, "remote_field") and getattr(new_field.remote_field, "model", None):


self.renamed_fields.get(rename_key + (to_field,), to_field)                         for to_field in new_field.to_fields                     ])<ins>                dependencies.extend(self._get_dependencies_for_foreign_key(new_field))</ins>             if hasattr(new_field, "remote_field") and getattr(new_field.remote_field, "through", None):                 rename_key = (                     new_field.remote_field.through._meta.app_label,


name=field_name,                             field=field,                             preserve_default=preserve_default,                        )                        ),                        dependencies=dependencies,                     )                 else:                     # We cannot alter between m2m and concrete fields


)             ] <ins>        choice_max_length = 0</ins>         # Expect [group_name, [value, display]]         for choices_group in self.choices:             try:


for value, human_name in group_choices                 ):                     break                if self.max_length is not None and group_choices:                    choice_max_length = max(                        choice_max_length,                        *(len(value) for value, _ in group_choices if isinstance(value, str)),                    )             except (TypeError, ValueError):                 # No groups, choices in the form [value, display]                 value, human_name = group_name, group_choices                 if not is_value(value) or not is_value(human_name):                     break                if self.max_length is not None and isinstance(value, str):                    choice_max_length = max(choice_max_length, len(value))              # Special case: choices=['ab']             if isinstance(choices_group, str):                 break         else:            if self.max_length is not None and choice_max_length > self.max_length:                return [                    checks.Error(                        "'max_length' is too small to fit the longest value "                        "in 'choices' (%d characters)." % choice_max_length,                        obj=self,                        id='fields.E009',                    ),                ]             return []          return [


yield opt      parser_actions = list(get_actions(parser))    mutually_exclusive_required_options = {        opt        for group in parser._mutually_exclusive_groups        for opt in group._group_actions if group.required    }     # Any required arguments which are passed in via **options must be passed     # to parse_args().     parse_args += [         '{}={}'.format(min(opt.option_strings), arg_options[opt.dest])        for opt in parser_actions if opt.required and opt.dest in options        for opt in parser_actions if (            opt.dest in options and            (opt.required or opt in mutually_exclusive_required_options)        )     ]     defaults = parser.parse_args(args=parse_args)     defaults = dict(defaults._get_kwargs(), **arg_options)


DEFAULT_INDEX_TABLESPACE = ''  # Default X-Frame-Options header valueX_FRAME_OPTIONS = 'SAMEORIGIN'X_FRAME_OPTIONS = 'DENY'  USE_X_FORWARDED_HOST = False USE_X_FORWARDED_PORT = Falsediff --git a/django/core/checks/security/base.py b/django/core/checks/security/base.py-- a/django/core/checks/security/base.py++ b/django/core/checks/security/base.py


"You have "     "'django.middleware.clickjacking.XFrameOptionsMiddleware' in your "     "MIDDLEWARE, but X_FRAME_OPTIONS is not set to 'DENY'. "    "The default is 'SAMEORIGIN', but unless there is a good reason for "    "your site to serve other parts of itself in a frame, you should "    "change it to 'DENY'.",    "Unless there is a good reason for your site to serve other parts of "    "itself in a frame, you should change it to 'DENY'.",     id='security.W019', ) diff --git a/django/middleware/clickjacking.py b/django/middleware/clickjacking.py-- a/django/middleware/clickjacking.py++ b/django/middleware/clickjacking.py


def get_xframe_options_value(self, request, response):         """         Get the value to set for the X_FRAME_OPTIONS header. Use the value from        the X_FRAME_OPTIONS setting, or 'SAMEORIGIN' if not set.        the X_FRAME_OPTIONS setting, or 'DENY' if not set.          This method can be overridden if needed, allowing it to vary based on         the request or response.         """        return getattr(settings, 'X_FRAME_OPTIONS', 'SAMEORIGIN').upper()        return getattr(settings, 'X_FRAME_OPTIONS', 'DENY').upper()


# Custom logging configuration. LOGGING = {} # Default exception reporter class used in case none has been# specifically assigned to the HttpRequest instance.DEFAULT_EXCEPTION_REPORTER = 'django.views.debug.ExceptionReporter' # Default exception reporter filter class used in case none has been # specifically assigned to the HttpRequest instance. DEFAULT_EXCEPTION_REPORTER_FILTER = 'django.views.debug.SafeExceptionReporterFilter'diff --git a/django/utils/log.py b/django/utils/log.py-- a/django/utils/log.py++ b/django/utils/log.py


super().__init__()         self.include_html = include_html         self.email_backend = email_backend        self.reporter_class = import_string(reporter_class or 'django.views.debug.ExceptionReporter')        self.reporter_class = import_string(reporter_class or settings.DEFAULT_EXCEPTION_REPORTER)      def emit(self, record):         try:diff --git a/django/views/debug.py b/django/views/debug.py-- a/django/views/debug.py++ b/django/views/debug.py


Create a technical server error response. The last three arguments are     the values returned from sys.exc_info() and friends.     """    reporter = ExceptionReporter(request, exc_type, exc_value, tb)    reporter = get_exception_reporter_class(request)(request, exc_type, exc_value, tb)     if request.is_ajax():         text = reporter.get_traceback_text()         return HttpResponse(text, status=status_code, content_type='text/plain; charset=utf-8')


return getattr(request, 'exception_reporter_filter', default_filter)  <ins>def get_exception_reporter_class(request):    default_exception_reporter_class = import_string(settings.DEFAULT_EXCEPTION_REPORTER)    return getattr(request, 'exception_reporter_class', default_exception_reporter_class)</ins> class SafeExceptionReporterFilter:     """     Use annotations made by the sensitive_post_parameters and


import hashlibfrom urllib.parse import quote  TEMPLATE_FRAGMENT_KEY_TEMPLATE = 'template.cache.%s.%s'   def make_template_fragment_key(fragment_name, vary_on=None):    if vary_on is None:        vary_on = ()    key = ':'.join(quote(str(var)) for var in vary_on)    args = hashlib.md5(key.encode())    return TEMPLATE_FRAGMENT_KEY_TEMPLATE % (fragment_name, args.hexdigest())    hasher = hashlib.md5()    if vary_on is not None:        for arg in vary_on:            hasher.update(str(arg).encode())            hasher.update(b':')    return TEMPLATE_FRAGMENT_KEY_TEMPLATE % (fragment_name, hasher.hexdigest())


# Set the max length and label for the "username" field.         self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)        self.fields['username'].max_length = self.username_field.max_length or 254        username_max_length = self.username_field.max_length or 254        self.fields['username'].max_length = username_max_length        self.fields['username'].widget.attrs['maxlength'] = username_max_length         if self.fields['username'].label is None:             self.fields['username'].label = capfirst(self.username_field.verbose_name)


from django.db.models.sql.query import Query         if isinstance(self.rhs, Query):             if self.rhs.has_limit_one():                # The subquery must select only the pk.                self.rhs.clear_select_clause()                self.rhs.add_fields(['pk'])                if not self.rhs.has_select_fields:                    self.rhs.clear_select_clause()                    self.rhs.add_fields(['pk'])             else:                 raise ValueError(                     'The QuerySet value for an exact lookup must be limited to '


databases). The abstraction barrier only works one way: this module has to know all about the internals of models in order to get the information it needs. """<ins>import copy</ins> import difflib import functools import inspect


obj._extra_select_cache = None         else:             obj._extra_select_cache = self._extra_select_cache.copy()<ins>        if self.select_related is not False:            # Use deepcopy because select_related stores fields in nested            # dicts.            obj.select_related = copy.deepcopy(obj.select_related)</ins>         if 'subq_aliases' in self.__dict__:             obj.subq_aliases = self.subq_aliases.copy()         obj.used_aliases = self.used_aliases.copy()


def serialize(self):         enum_class = self.value.__class__         module = enum_class.__module__        v_string, v_imports = serializer_factory(self.value.value).serialize()        imports = {'import %s' % module, *v_imports}        return "%s.%s(%s)" % (module, enum_class.__name__, v_string), imports        return (            '%s.%s[%r]' % (module, enum_class.__name__, self.value.name),            {'import %s' % module},        )   class FloatSerializer(BaseSimpleSerializer):


fld = None             for part in field.split(LOOKUP_SEP):                 try:                    fld = _cls._meta.get_field(part)                    # pk is an alias that won't be found by opts.get_field.                    if part == 'pk':                        fld = _cls._meta.pk                    else:                        fld = _cls._meta.get_field(part)                     if fld.is_relation:                         _cls = fld.get_path_info()[-1].to_opts.model                    else:                        _cls = None                 except (FieldDoesNotExist, AttributeError):                     if fld is None or fld.get_transform(part) is None:                         errors.append(


"""     def __init__(self, get_response=None):         self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS<ins>        self.page_timeout = None</ins>         self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX         self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS         self.cache = caches[self.cache_alias]


if 'private' in response.get('Cache-Control', ()):             return response         # Try to get the timeout from the "max-age" section of the "Cache-        # Control" header before reverting to using the default cache_timeout        # length.        timeout = get_max_age(response)        # Page timeout takes precedence over the "max-age" and the default        # cache timeout.        timeout = self.page_timeout         if timeout is None:            timeout = self.cache_timeout        elif timeout == 0:            # max-age was set to 0, don't bother caching.            return response            # The timeout from the "max-age" section of the "Cache-Control"            # header takes precedence over the default cache timeout.            timeout = get_max_age(response)            if timeout is None:                timeout = self.cache_timeout            elif timeout == 0:                # max-age was set to 0, don't cache.                return response         patch_response_headers(response, timeout)         if timeout and response.status_code == 200:             cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)


Also used as the hook point for the cache decorator, which is generated     using the decorator-from-middleware utility.     """    def __init__(self, get_response=None, cache_timeout=None, **kwargs):    def __init__(self, get_response=None, cache_timeout=None, page_timeout=None, **kwargs):         self.get_response = get_response         # We need to differentiate between "provided, but using default value",         # and "not provided". If the value is provided using a default, then


if cache_timeout is None:             cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS         self.cache_timeout = cache_timeout        self.page_timeout = page_timeout         self.cache = caches[self.cache_alias]diff --git a/django/views/decorators/cache.py b/django/views/decorators/cache.py-- a/django/views/decorators/cache.py++ b/django/views/decorators/cache.py


into account on caching -- just like the middleware does.     """     return decorator_from_middleware_with_args(CacheMiddleware)(        cache_timeout=timeout, cache_alias=cache, key_prefix=key_prefix        page_timeout=timeout, cache_alias=cache, key_prefix=key_prefix,     )


import hashlib import re import time<ins>from collections import defaultdict</ins>  from django.conf import settings from django.core.cache import caches


else:             return (t[0].lower(), True)     def dictvalue(t):    def dictvalue(*t):         if t[1] is True:             return t[0]         else:             return '%s=%s' % (t[0], t[1])     cc = defaultdict(set)     if response.get('Cache-Control'):        cc = cc_delim_re.split(response['Cache-Control'])        cc = dict(dictitem(el) for el in cc)    else:        cc = {}        for field in cc_delim_re.split(response['Cache-Control']):            directive, value = dictitem(field)            if directive == 'no-cache':                # no-cache supports multiple field names.                cc[directive].add(value)            else:                cc[directive] = value      # If there's already a max-age header but we're being asked to set a new     # max-age, use the minimum of the two ages. In practice this happens when


del cc['public']      for (k, v) in kwargs.items():        cc[k.replace('_', '-')] = v    cc = ', '.join(dictvalue(el) for el in cc.items())        directive = k.replace('_', '-')        if directive == 'no-cache':            # no-cache supports multiple field names.            cc[directive].add(v)        else:            cc[directive] = v    directives = []    for directive, values in cc.items():        if isinstance(values, set):            if True in values:                # True takes precedence.                values = {True}            directives.extend([dictvalue(directive, value) for value in values])        else:            directives.append(dictvalue(directive, values))    cc = ', '.join(directives)     response['Cache-Control'] = cc


try:         year = int(m.group('year'))         if year < 100:            if year < 70:                year += 2000            current_year = datetime.datetime.utcnow().year            current_century = current_year - (current_year % 100)            if year - (current_year % 100) > 50:                # year that appears to be more than 50 years in the future are                # interpreted as representing the past.                year += current_century - 100             else:                year += 1900                year += current_century         month = MONTHS.index(m.group('mon').lower()) + 1         day = int(m.group('day'))         hour = int(m.group('hour'))


result = copy.copy(self)         memo[id(self)] = result         result.widget = copy.deepcopy(self.widget, memo)<ins>        result.error_messages = self.error_messages.copy()</ins>         result.validators = self.validators[:]         return result


def delete(self, key, version=None):         """        Delete a key from the cache, failing silently.        Delete a key from the cache and return whether it succeeded, failing        silently.         """         raise NotImplementedError('subclasses of BaseCache must provide a delete() method') diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py-- a/django/core/cache/backends/db.py++ b/django/core/cache/backends/db.py


return True      def delete(self, key, version=None):        self.delete_many([key], version)        self.validate_key(key)        return self._base_delete_many([self.make_key(key, version)])      def delete_many(self, keys, version=None):         key_list = []


def _base_delete_many(self, keys):         if not keys:            return            return False          db = router.db_for_write(self.cache_model_class)         connection = connections[db]


),                 keys,             )        return bool(cursor.rowcount)      def has_key(self, key, version=None):         key = self.make_key(key, version=version)diff --git a/django/core/cache/backends/dummy.py b/django/core/cache/backends/dummy.py-- a/django/core/cache/backends/dummy.py++ b/django/core/cache/backends/dummy.py


def delete(self, key, version=None):         key = self.make_key(key, version=version)         self.validate_key(key)        return False      def has_key(self, key, version=None):         key = self.make_key(key, version=version)diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py-- a/django/core/cache/backends/filebased.py++ b/django/core/cache/backends/filebased.py


return False      def delete(self, key, version=None):        self._delete(self._key_to_file(key, version))        return self._delete(self._key_to_file(key, version))      def _delete(self, fname):         if not fname.startswith(self._dir) or not os.path.exists(fname):            return            return False         try:             os.remove(fname)         except FileNotFoundError:             # The file may have been removed by another process.            pass            return False        return True      def has_key(self, key, version=None):         fname = self._key_to_file(key, version)diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py-- a/django/core/cache/backends/locmem.py++ b/django/core/cache/backends/locmem.py


del self._cache[key]             del self._expire_info[key]         except KeyError:            pass            return False        return True      def delete(self, key, version=None):         key = self.make_key(key, version=version)         self.validate_key(key)         with self._lock:            self._delete(key)            return self._delete(key)      def clear(self):         with self._lock:diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py-- a/django/core/cache/backends/memcached.py++ b/django/core/cache/backends/memcached.py


def delete(self, key, version=None):         key = self.make_key(key, version=version)        self._cache.delete(key)        return bool(self._cache.delete(key))      def get_many(self, keys, version=None):         key_map = {self.make_key(key, version=version): key for key in keys}


return default         return val <ins>    def delete(self, key, version=None):        # python-memcached's delete() returns True when key doesn't exist.        # https://github.com/linsomniac/python-memcached/issues/170        # Call _deletetouch() without the NOT_FOUND in expected results.        key = self.make_key(key, version=version)        return bool(self._cache._deletetouch([b'DELETED'], 'delete', key))</ins>  class PyLibMCCache(BaseMemcachedCache):     "An implementation of a cache binding using pylibmc"


except models.ProtectedError as e:             self.protected.update(e.protected_objects)     def related_objects(self, related, objs):        qs = super().related_objects(related, objs)        return qs.select_related(related.field.name)    def related_objects(self, related_model, related_fields, objs):        qs = super().related_objects(related_model, related_fields, objs)        return qs.select_related(*[related_field.name for related_field in related_fields])      def _nested(self, obj, seen, format_callback):         if obj in seen:diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py-- a/django/db/models/deletion.py++ b/django/db/models/deletion.py


from collections import Counterimport operatorfrom collections import Counter, defaultdictfrom functools import partial, reduce from itertools import chain from operator import attrgetter  from django.db import IntegrityError, connections, transactionfrom django.db.models import signals, sqlfrom django.db.models import query_utils, signals, sql   class ProtectedError(IntegrityError):


def __init__(self, using):         self.using = using         # Initially, {model: {instances}}, later values become lists.        self.data = {}        self.field_updates = {}  # {model: {(field, value): {instances}}}        self.data = defaultdict(set)        # {model: {(field, value): {instances}}}        self.field_updates = defaultdict(partial(defaultdict, set))         # fast_deletes is a list of queryset-likes that can be deleted without         # fetching the objects into memory.         self.fast_deletes = []


# should be included, as the dependencies exist only between actual         # database tables; proxy models are represented here by their concrete         # parent.        self.dependencies = {}  # {model: {models}}        self.dependencies = defaultdict(set)  # {model: {models}}      def add(self, objs, source=None, nullable=False, reverse_dependency=False):         """


return []         new_objs = []         model = objs[0].__class__        instances = self.data.setdefault(model, set())        instances = self.data[model]         for obj in objs:             if obj not in instances:                 new_objs.append(obj)


if source is not None and not nullable:             if reverse_dependency:                 source, model = model, source            self.dependencies.setdefault(                source._meta.concrete_model, set()).add(model._meta.concrete_model)            self.dependencies[source._meta.concrete_model].add(model._meta.concrete_model)         return new_objs      def add_field_update(self, field, value, objs):


if not objs:             return         model = objs[0].__class__        self.field_updates.setdefault(            model, {}).setdefault(            (field, value), set()).update(objs)        self.field_updates[model][field, value].update(objs)      def _has_signal_listeners(self, model):         return (


if from_field and from_field.remote_field.on_delete is not CASCADE:             return False         if hasattr(objs, '_meta'):            model = type(objs)            model = objs._meta.model         elif hasattr(objs, 'model') and hasattr(objs, '_raw_delete'):             model = objs.model         else:


)         )     def get_del_batches(self, objs, field):    def get_del_batches(self, objs, fields):         """         Return the objs in suitably sized batches for the used connection.         """        field_names = [field.name for field in fields]         conn_batch_size = max(            connections[self.using].ops.bulk_batch_size([field.name], objs), 1)            connections[self.using].ops.bulk_batch_size(field_names, objs), 1)         if len(objs) > conn_batch_size:             return [objs[i:i + conn_batch_size]                     for i in range(0, len(objs), conn_batch_size)]


source_attr=ptr.remote_field.related_name,                                  collect_related=False,                                  reverse_dependency=True)        if collect_related:            if keep_parents:                parents = set(model._meta.get_parent_list())            for related in get_candidate_relations_to_delete(model._meta):                # Preserve parent reverse relationships if keep_parents=True.                if keep_parents and related.model in parents:                    continue                field = related.field                if field.remote_field.on_delete == DO_NOTHING:                    continue                batches = self.get_del_batches(new_objs, field)                for batch in batches:                    sub_objs = self.related_objects(related, batch)                    if self.can_fast_delete(sub_objs, from_field=field):                        self.fast_deletes.append(sub_objs)                    else:                        related_model = related.related_model                        # Non-referenced fields can be deferred if no signal                        # receivers are connected for the related model as                        # they'll never be exposed to the user. Skip field                        # deferring when some relationships are select_related                        # as interactions between both features are hard to                        # get right. This should only happen in the rare                        # cases where .related_objects is overridden anyway.                        if not (sub_objs.query.select_related or self._has_signal_listeners(related_model)):                            referenced_fields = set(chain.from_iterable(                                (rf.attname for rf in rel.field.foreign_related_fields)                                for rel in get_candidate_relations_to_delete(related_model._meta)                            ))                            sub_objs = sub_objs.only(*tuple(referenced_fields))                        if sub_objs:                            field.remote_field.on_delete(self, field, sub_objs, self.using)            for field in model._meta.private_fields:                if hasattr(field, 'bulk_related_objects'):                    # It's something like generic foreign key.                    sub_objs = field.bulk_related_objects(new_objs, self.using)                    self.collect(sub_objs, source=model, nullable=True)    def related_objects(self, related, objs):        if not collect_related:            return        if keep_parents:            parents = set(model._meta.get_parent_list())        model_fast_deletes = defaultdict(list)        for related in get_candidate_relations_to_delete(model._meta):            # Preserve parent reverse relationships if keep_parents=True.            if keep_parents and related.model in parents:                continue            field = related.field            if field.remote_field.on_delete == DO_NOTHING:                continue            related_model = related.related_model            if self.can_fast_delete(related_model, from_field=field):                model_fast_deletes[related_model].append(field)                continue            batches = self.get_del_batches(new_objs, [field])            for batch in batches:                sub_objs = self.related_objects(related_model, [field], batch)                # Non-referenced fields can be deferred if no signal receivers                # are connected for the related model as they'll never be                # exposed to the user. Skip field deferring when some                # relationships are select_related as interactions between both                # features are hard to get right. This should only happen in                # the rare cases where .related_objects is overridden anyway.                if not (sub_objs.query.select_related or self._has_signal_listeners(related_model)):                    referenced_fields = set(chain.from_iterable(                        (rf.attname for rf in rel.field.foreign_related_fields)                        for rel in get_candidate_relations_to_delete(related_model._meta)                    ))                    sub_objs = sub_objs.only(*tuple(referenced_fields))                if sub_objs:                    field.remote_field.on_delete(self, field, sub_objs, self.using)        for related_model, related_fields in model_fast_deletes.items():            batches = self.get_del_batches(new_objs, related_fields)            for batch in batches:                sub_objs = self.related_objects(related_model, related_fields, batch)                self.fast_deletes.append(sub_objs)        for field in model._meta.private_fields:            if hasattr(field, 'bulk_related_objects'):                # It's something like generic foreign key.                sub_objs = field.bulk_related_objects(new_objs, self.using)                self.collect(sub_objs, source=model, nullable=True)    def related_objects(self, related_model, related_fields, objs):         """        Get a QuerySet of objects related to `objs` via the relation `related`.        Get a QuerySet of the related model to objs via related fields.         """        return related.related_model._base_manager.using(self.using).filter(            **{"%s__in" % related.field.name: objs}        )        predicate = reduce(operator.or_, (            query_utils.Q(**{'%s__in' % related_field.name: objs})            for related_field in related_fields        ))        return related_model._base_manager.using(self.using).filter(predicate)      def instances_with_model(self):         for model, instances in self.data.items():


def set_response_etag(response):    if not response.streaming:    if not response.streaming and response.content:         response['ETag'] = quote_etag(hashlib.md5(response.content).hexdigest())     return response


URLInput, ) from django.utils import formatsfrom django.utils.dateparse import parse_durationfrom django.utils.dateparse import parse_datetime, parse_duration from django.utils.duration import duration_string from django.utils.ipv6 import clean_ipv6_address from django.utils.regex_helper import _lazy_re_compile


if isinstance(value, datetime.date):             result = datetime.datetime(value.year, value.month, value.day)             return from_current_timezone(result)        result = super().to_python(value)        try:            result = parse_datetime(value.strip())        except ValueError:            raise ValidationError(self.error_messages['invalid'], code='invalid')        if not result:            result = super().to_python(value)         return from_current_timezone(result)      def strptime(self, value, format):


if httponly:             self.cookies[key]['httponly'] = True         if samesite:            if samesite.lower() not in ('lax', 'strict'):                raise ValueError('samesite must be "lax" or "strict".')            if samesite.lower() not in ('lax', 'none', 'strict'):                raise ValueError('samesite must be "lax", "none", or "strict".')             self.cookies[key]['samesite'] = samesite      def setdefault(self, key, value):


# (get_commands() swallows the original one) so the user is                 # informed about it.                 settings.INSTALLED_APPS            else:            elif not settings.configured:                 sys.stderr.write("No Django settings specified.\n")             possible_matches = get_close_matches(subcommand, commands)             sys.stderr.write('Unknown command: %r' % subcommand)


import itertools import math<ins>import warnings</ins> from copy import copy  from django.core.exceptions import EmptyResultSet


) from django.db.models.query_utils import RegisterLookupMixin from django.utils.datastructures import OrderedSet<ins>from django.utils.deprecation import RemovedInDjango40Warning</ins> from django.utils.functional import cached_property


prepare_rhs = False      def as_sql(self, compiler, connection):<ins>        if not isinstance(self.rhs, bool):            # When the deprecation ends, replace with:            # raise ValueError(            #     'The QuerySet value for an isnull lookup must be True or '            #     'False.'            # )            warnings.warn(                'Using a non-boolean value for an isnull lookup is '                'deprecated, use True or False instead.',                RemovedInDjango40Warning,            )</ins>         sql, params = compiler.compile(self.lhs)         if self.rhs:             return "%s IS NULL" % sql, params


if remote_field_name:                     to_field_rename_key = rename_key + (remote_field_name,)                     if to_field_rename_key in self.renamed_fields:<ins>                        # Repoint both model and field name because to_field                        # inclusion in ForeignKey.deconstruct() is based on                        # both.                        new_field.remote_field.model = old_field.remote_field.model</ins>                         new_field.remote_field.field_name = old_field.remote_field.field_name                 # Handle ForeignObjects which can have multiple from_fields/to_fields.                 from_fields = getattr(new_field, 'from_fields', None)


def describe_operation(operation, backwards):         """Return a string that describes a migration operation for --plan."""         prefix = ''        is_error = False         if hasattr(operation, 'code'):             code = operation.reverse_code if backwards else operation.code            action = code.__doc__ if code else ''            action = (code.__doc__ or '') if code else None         elif hasattr(operation, 'sql'):             action = operation.reverse_sql if backwards else operation.sql         else:             action = ''             if backwards:                 prefix = 'Undo '        if action is None:        if action is not None:            action = str(action).replace('\n', '')        elif backwards:             action = 'IRREVERSIBLE'             is_error = True        else:            action = str(action).replace('\n', '')            is_error = False         if action:             action = ' -> ' + action         truncated = Truncator(action)


queryset._add_hints(instance=self.instance)             if self._db:                 queryset = queryset.using(self._db)            queryset._defer_next_filter = True             return queryset._next_is_sticky().filter(**self.core_filters)          def _remove_prefetched_objects(self):diff --git a/django/db/models/query.py b/django/db/models/query.py-- a/django/db/models/query.py++ b/django/db/models/query.py


self.model = model         self._db = using         self._hints = hints or {}        self.query = query or sql.Query(self.model)        self._query = query or sql.Query(self.model)         self._result_cache = None         self._sticky_filter = False         self._for_write = False


self._known_related_objects = {}  # {rel_field: {pk: rel_obj}}         self._iterable_class = ModelIterable         self._fields = None<ins>        self._defer_next_filter = False        self._deferred_filter = None    @property    def query(self):        if self._deferred_filter:            negate, args, kwargs = self._deferred_filter            self._filter_or_exclude_inplace(negate, *args, **kwargs)            self._deferred_filter = None        return self._query    @query.setter    def query(self, value):        self._query = value</ins>      def as_manager(cls):         # Address the circular dependency between `Queryset` and `Manager`.


"Cannot filter a query once a slice has been taken."          clone = self._chain()        if negate:            clone.query.add_q(~Q(*args, **kwargs))        if self._defer_next_filter:            self._defer_next_filter = False            clone._deferred_filter = negate, args, kwargs         else:            clone.query.add_q(Q(*args, **kwargs))            clone._filter_or_exclude_inplace(negate, *args, **kwargs)         return clone     def _filter_or_exclude_inplace(self, negate, *args, **kwargs):        if negate:            self._query.add_q(~Q(*args, **kwargs))        else:            self._query.add_q(Q(*args, **kwargs))     def complex_filter(self, filter_obj):         """         Return a new QuerySet instance with filter_obj added to the filters.


clone.query.add_q(filter_obj)             return clone         else:            return self._filter_or_exclude(None, **filter_obj)            return self._filter_or_exclude(False, **filter_obj)      def _combinator_query(self, combinator, *other_qs, all=False):         # Clone the query to inherit the select list and everything


if ignore_conflicts and not connections[self.db].features.supports_ignore_conflicts:             raise NotSupportedError('This database backend does not support ignoring conflicts.')         ops = connections[self.db].ops        batch_size = (batch_size or max(ops.bulk_batch_size(fields, objs), 1))        max_batch_size = max(ops.bulk_batch_size(fields, objs), 1)        batch_size = min(batch_size, max_batch_size) if batch_size else max_batch_size         inserted_rows = []         bulk_return = connections[self.db].features.can_return_rows_from_bulk_insert         for item in [objs[i:i + batch_size] for i in range(0, len(objs), batch_size)]:


class Choices(enum.Enum, metaclass=ChoicesMeta):     """Class for creating enumerated choices."""    pass    def __str__(self):        """        Use value when cast to str, so that Choices set as model instance        attributes are rendered as expected in templates and similar contexts.        """        return str(self.value)   class IntegerChoices(int, Choices):


)                 except ValueError as e:                     raise IncorrectLookupParameters(e) from e<del>                if settings.USE_TZ:                    from_date = make_aware(from_date)</del>                 if day:                     to_date = from_date + timedelta(days=1)                 elif month:


to_date = (from_date + timedelta(days=32)).replace(day=1)                 else:                     to_date = from_date.replace(year=from_date.year + 1)<ins>                if settings.USE_TZ:                    from_date = make_aware(from_date)                    to_date = make_aware(to_date)</ins>                 lookup_params.update({                     '%s__gte' % self.date_hierarchy: from_date,                     '%s__lt' % self.date_hierarchy: to_date,


# Avoid conversion to scientific notation by accessing `sign`, `digits`,     # and `exponent` from Decimal.as_tuple() directly.    sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec)).as_tuple()    rounded_d = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec))    sign, digits, exponent = rounded_d.as_tuple()     digits = [str(digit) for digit in reversed(digits)]     while len(digits) <= abs(exponent):         digits.append('0')     digits.insert(-exponent, '.')    if sign:    if sign and rounded_d:         digits.append('-')     number = ''.join(reversed(digits))     return mark_safe(formats.number_format(number, abs(p)))


if not getattr(cls, self.attname, None):                 setattr(cls, self.attname, self.descriptor_class(self))         if self.choices is not None:            setattr(cls, 'get_%s_display' % self.name,                    partialmethod(cls._get_FIELD_display, field=self))            if not hasattr(cls, 'get_%s_display' % self.name):                setattr(                    cls,                    'get_%s_display' % self.name,                    partialmethod(cls._get_FIELD_display, field=self),                )      def get_filter_kwargs_for_object(self, obj):         """


#!/usr/bin/env python# When the django-admin.py deprecation ends, remove this script.import warnings from django.core import management try:    from django.utils.deprecation import RemovedInDjango40Warningexcept ImportError:    raise ImportError(        'django-admin.py was deprecated in Django 3.1 and removed in Django '        '4.0. Please manually remove this script from your virtual environment '        'and use django-admin instead.'    ) if __name__ == "__main__":    warnings.warn(        'django-admin.py is deprecated in favor of django-admin.',        RemovedInDjango40Warning,    )     management.execute_from_command_line()


def __str__(self):         def col_str(column, idx):            col = self.quote_name(column)             try:                return self.quote_name(column) + self.col_suffixes[idx]                suffix = self.col_suffixes[idx]                if suffix:                    col = '{} {}'.format(col, suffix)             except IndexError:                return self.quote_name(column)                pass            return col          return ', '.join(col_str(column, idx) for idx, column in enumerate(self.columns))


# length as self.columns.             col = '{} {}'.format(self.quote_name(column), self.opclasses[idx])             try:                col = '{} {}'.format(col, self.col_suffixes[idx])                suffix = self.col_suffixes[idx]                if suffix:                    col = '{} {}'.format(col, suffix)             except IndexError:                 pass             return col


apps = after_state.apps         found_create_model_migration = False         found_add_field_migration = False        fold_identifier_case = self.connection.features.ignores_table_name_case         with self.connection.cursor() as cursor:            existing_table_names = self.connection.introspection.table_names(cursor)            existing_table_names = set(self.connection.introspection.table_names(cursor))            if fold_identifier_case:                existing_table_names = {name.casefold() for name in existing_table_names}         # Make sure all create model and add field operations are done         for operation in migration.operations:             if isinstance(operation, migrations.CreateModel):


model = global_apps.get_model(model._meta.swapped)                 if should_skip_detecting_model(migration, model):                     continue                if model._meta.db_table not in existing_table_names:                db_table = model._meta.db_table                if fold_identifier_case:                    db_table = db_table.casefold()                if db_table not in existing_table_names:                     return False, project_state                 found_create_model_migration = True             elif isinstance(operation, migrations.AddField):


# Handle implicit many-to-many tables created by AddField.                 if field.many_to_many:                    if field.remote_field.through._meta.db_table not in existing_table_names:                    through_db_table = field.remote_field.through._meta.db_table                    if fold_identifier_case:                        through_db_table = through_db_table.casefold()                    if through_db_table not in existing_table_names:                         return False, project_state                     else:                         found_add_field_migration = True                         continue                column_names = [                    column.name for column in                    self.connection.introspection.get_table_description(self.connection.cursor(), table)                ]                if field.column not in column_names:                columns = self.connection.introspection.get_table_description(                    self.connection.cursor(),                    table,                )                for column in columns:                    field_column = field.column                    column_name = column.name                    if fold_identifier_case:                        column_name = column_name.casefold()                        field_column = field_column.casefold()                    if column_name == field_column:                        found_add_field_migration = True                        break                else:                     return False, project_state                found_add_field_migration = True         # If we get this far and we found at least one CreateModel or AddField migration,         # the migration is considered implicitly applied.         return (found_create_model_migration or found_add_field_migration), after_state


elif isinstance(value, (list, tuple)):             # The items of the iterable may be expressions and therefore need             # to be resolved independently.            resolved_values = []            for sub_value in value:                if hasattr(sub_value, 'resolve_expression'):                    if isinstance(sub_value, F):                        resolved_values.append(sub_value.resolve_expression(                            self, reuse=can_reuse, allow_joins=allow_joins,                            simple_col=simple_col,                        ))                    else:                        resolved_values.append(sub_value.resolve_expression(                            self, reuse=can_reuse, allow_joins=allow_joins,                        ))                else:                    resolved_values.append(sub_value)            value = tuple(resolved_values)            return type(value)(                self.resolve_lookup_value(sub_value, can_reuse, allow_joins, simple_col)                for sub_value in value            )         return value      def solve_lookup_type(self, lookup):


settings.DATABASES[self.connection.alias]["NAME"] = test_database_name         self.connection.settings_dict["NAME"] = test_database_name         # We report migrate messages at one level lower than that requested.        # This ensures we don't get flooded with messages during testing        # (unless you really ask to be flooded).        call_command(            'migrate',            verbosity=max(verbosity - 1, 0),            interactive=False,            database=self.connection.alias,            run_syncdb=True,        )        if self.connection.settings_dict['TEST']['MIGRATE']:            # We report migrate messages at one level lower than that            # requested. This ensures we don't get flooded with messages during            # testing (unless you really ask to be flooded).            call_command(                'migrate',                verbosity=max(verbosity - 1, 0),                interactive=False,                database=self.connection.alias,                run_syncdb=True,            )          # We then serialize the current state of the database into a string         # and store it on the connection. This slightly horrific process is so peoplediff --git a/django/db/utils.py b/django/db/utils.py-- a/django/db/utils.py++ b/django/db/utils.py


raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)          test_settings = conn.setdefault('TEST', {})        for key in ['CHARSET', 'COLLATION', 'NAME', 'MIRROR']:            test_settings.setdefault(key, None)        default_test_settings = [            ('CHARSET', None),            ('COLLATION', None),            ('MIGRATE', True),            ('MIRROR', None),            ('NAME', None),        ]        for key, value in default_test_settings:            test_settings.setdefault(key, value)      def __getitem__(self, alias):         if hasattr(self._connections, alias):


from django.db.models.expressions import Case, Expression, F, Value, When from django.db.models.fields import AutoField from django.db.models.functions import Cast, Truncfrom django.db.models.query_utils import FilteredRelation, InvalidQuery, Qfrom django.db.models.query_utils import FilteredRelation, Q from django.db.models.sql.constants import CURSOR, GET_ITERATOR_CHUNK_SIZE from django.db.utils import NotSupportedError from django.utils import timezone


try:             model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()             if self.model._meta.pk.attname not in model_init_names:                raise InvalidQuery('Raw query must include the primary key')                raise exceptions.FieldDoesNotExist(                    'Raw query must include the primary key'                )             model_cls = self.model             fields = [self.model_fields.get(c) for c in self.columns]             converters = compiler.get_converters([diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py-- a/django/db/models/query_utils.py++ b/django/db/models/query_utils.py


import copy import functools import inspectimport warnings from collections import namedtuple from django.core.exceptions import FieldDoesNotExist, FieldError from django.db.models.constants import LOOKUP_SEP from django.utils import treefrom django.utils.deprecation import RemovedInDjango40Warning  # PathInfo is used when converting lookups (fk__somecol). The contents # describe the relation in Model terms (model Options and Fields for both


PathInfo = namedtuple('PathInfo', 'from_opts to_opts target_fields join_field m2m direct filtered_relation')  class InvalidQuery(Exception):    """The query passed to raw() isn't a safe query to use with raw()."""class InvalidQueryType(type):    @property    def _subclasses(self):        return (FieldDoesNotExist, FieldError)    def __warn(self):        warnings.warn(            'The InvalidQuery exception class is deprecated. Use '            'FieldDoesNotExist or FieldError instead.',            category=RemovedInDjango40Warning,            stacklevel=4,        )    def __instancecheck__(self, instance):        self.__warn()        return isinstance(instance, self._subclasses) or super().__instancecheck__(instance)    def __subclasscheck__(self, subclass):        self.__warn()        return issubclass(subclass, self._subclasses) or super().__subclasscheck__(subclass)class InvalidQuery(Exception, metaclass=InvalidQueryType):     pass


if load_fields:         if field.attname not in load_fields:             if restricted and field.name in requested:                raise InvalidQuery("Field %s.%s cannot be both deferred"                                   " and traversed using select_related"                                   " at the same time." %                                   (field.model._meta.object_name, field.name))                msg = (                    'Field %s.%s cannot be both deferred and traversed using '                    'select_related at the same time.'                ) % (field.model._meta.object_name, field.name)                raise FieldError(msg)     return True


import cgi import codecs import copy<ins>import warnings</ins> from io import BytesIO from itertools import chain from urllib.parse import quote, urlencode, urljoin, urlsplit


from django.utils.datastructures import (     CaseInsensitiveMapping, ImmutableList, MultiValueDict, )<ins>from django.utils.deprecation import RemovedInDjango40Warning</ins> from django.utils.encoding import escape_uri_path, iri_to_uri from django.utils.functional import cached_property from django.utils.http import is_same_domain, limited_parse_qsl


return self.scheme == 'https'      def is_ajax(self):        warnings.warn(            'request.is_ajax() is deprecated. See Django 3.1 release notes '            'for more details about this deprecation.',            RemovedInDjango40Warning,        )         return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'      @propertydiff --git a/django/views/debug.py b/django/views/debug.py-- a/django/views/debug.py++ b/django/views/debug.py


the values returned from sys.exc_info() and friends.     """     reporter = get_exception_reporter_class(request)(request, exc_type, exc_value, tb)    if request.is_ajax():        text = reporter.get_traceback_text()        return HttpResponse(text, status=status_code, content_type='text/plain; charset=utf-8')    else:    if request.accepts('text/html'):         html = reporter.get_traceback_html()         return HttpResponse(html, status=status_code, content_type='text/html')    else:        text = reporter.get_traceback_text()        return HttpResponse(text, status=status_code, content_type='text/plain; charset=utf-8')   @functools.lru_cache()diff --git a/django/views/i18n.py b/django/views/i18n.py-- a/django/views/i18n.py++ b/django/views/i18n.py


"""     next_url = request.POST.get('next', request.GET.get('next'))     if (        (next_url or not request.is_ajax()) and        (next_url or request.accepts('text/html')) and         not url_has_allowed_host_and_scheme(             url=next_url,             allowed_hosts={request.get_host()},


sig = [self.connection.settings_dict['NAME']]         if self.is_in_memory_db(test_database_name):             sig.append(self.connection.alias)<ins>        else:            sig.append(test_database_name)</ins>         return tuple(sig)


candidate_subs = kwargs                 # Convert the candidate subs to text using Converter.to_url().                 text_candidate_subs = {}                match = True                 for k, v in candidate_subs.items():                     if k in converters:                        text_candidate_subs[k] = converters[k].to_url(v)                        try:                            text_candidate_subs[k] = converters[k].to_url(v)                        except ValueError:                            match = False                            break                     else:                         text_candidate_subs[k] = str(v)                if not match:                    continue                 # WSGI provides decoded URLs, without %xx escapes, and the URL                 # resolver operates on such URLs. First substitute arguments                 # without quoting to build a decoded URL and look for a match.


import calendar import datetime import timefrom email.utils import format_datetime as format_datetime_rfc5322  from django.utils.dates import (     MONTHS, MONTHS_3, MONTHS_ALT, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR, ) from django.utils.regex_helper import _lazy_re_compilefrom django.utils.timezone import get_default_timezone, is_aware, is_naivefrom django.utils.timezone import (    get_default_timezone, is_aware, is_naive, make_aware,) from django.utils.translation import gettext as _  re_formatchars = _lazy_re_compile(r'(?<!\\)([aAbcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])')


def r(self):         "RFC 5322 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"        return self.format('D, j M Y H:i:s O')        if type(self.data) is datetime.date:            raise TypeError(                "The format for date objects may not contain time-related "                "format specifiers (found 'r')."            )        if is_naive(self.data):            dt = make_aware(self.data, timezone=self.timezone)        else:            dt = self.data        return format_datetime_rfc5322(dt)      def S(self):         "English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'"


if module == builtins.__name__:                 return self.value.__name__, set()             else:                return "%s.%s" % (module, self.value.__name__), {"import %s" % module}                return "%s.%s" % (module, self.value.__qualname__), {"import %s" % module}   class UUIDSerializer(BaseSerializer):


# Allow variants of localhost if ALLOWED_HOSTS is empty and DEBUG=True.         allowed_hosts = settings.ALLOWED_HOSTS         if settings.DEBUG and not allowed_hosts:            allowed_hosts = ['localhost', '127.0.0.1', '[::1]']            allowed_hosts = ['.localhost', '127.0.0.1', '[::1]']          domain, port = split_domain_port(host)         if domain and validate_host(domain, allowed_hosts):


def _get_edited_object_pks(self, request, prefix):         """Return POST data values of list_editable primary keys."""        pk_pattern = re.compile(r'{}-\d+-{}$'.format(prefix, self.model._meta.pk.name))        pk_pattern = re.compile(            r'{}-\d+-{}$'.format(re.escape(prefix), self.model._meta.pk.name)        )         return [value for key, value in request.POST.items() if pk_pattern.match(key)]      def _get_list_editable_queryset(self, request, prefix):


from django.contrib.sites.models import Site from django.db import modelsfrom django.urls import get_script_prefixfrom django.urls import NoReverseMatch, get_script_prefix, reverse from django.utils.encoding import iri_to_uri from django.utils.translation import gettext_lazy as _


return "%s -- %s" % (self.url, self.title)      def get_absolute_url(self):<ins>        from .views import flatpage        for url in (self.url.lstrip('/'), self.url):            try:                return reverse(flatpage, kwargs={'url': url})            except NoReverseMatch:                pass</ins>         # Handle script prefix manually because we bypass reverse()         return iri_to_uri(get_script_prefix().rstrip('/') + self.url)


style = color_style()     Permission = apps.get_model('auth', 'Permission')     ContentType = apps.get_model('contenttypes', 'ContentType')<ins>    alias = schema_editor.connection.alias</ins>     for Model in apps.get_models():         opts = Model._meta         if not opts.proxy:


permissions_query = Q(codename__in=proxy_default_permissions_codenames)         for codename, name in opts.permissions:             permissions_query = permissions_query | Q(codename=codename, name=name)        concrete_content_type = ContentType.objects.get_for_model(Model, for_concrete_model=True)        proxy_content_type = ContentType.objects.get_for_model(Model, for_concrete_model=False)        content_type_manager = ContentType.objects.db_manager(alias)        concrete_content_type = content_type_manager.get_for_model(Model, for_concrete_model=True)        proxy_content_type = content_type_manager.get_for_model(Model, for_concrete_model=False)         old_content_type = proxy_content_type if reverse else concrete_content_type         new_content_type = concrete_content_type if reverse else proxy_content_type         try:            with transaction.atomic():                Permission.objects.filter(            with transaction.atomic(using=alias):                Permission.objects.using(alias).filter(                     permissions_query,                     content_type=old_content_type,                 ).update(content_type=new_content_type)


import re from email.errors import HeaderParseError from email.parser import HeaderParser<ins>from inspect import cleandoc</ins>  from django.urls import reverse from django.utils.regex_helper import _lazy_re_compile


return mod_name + '.' + view_name  def trim_docstring(docstring):    """    Uniformly trim leading/trailing whitespace from docstrings.    Based on https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation    """    if not docstring or not docstring.strip():        return ''    # Convert tabs to spaces and split into lines    lines = docstring.expandtabs().splitlines()    indent = min(len(line) - len(line.lstrip()) for line in lines if line.lstrip())    trimmed = [lines[0].lstrip()] + [line[indent:].rstrip() for line in lines[1:]]    return "\n".join(trimmed).strip() def parse_docstring(docstring):     """     Parse out the parts of a docstring.  Return (title, body, metadata).     """    docstring = trim_docstring(docstring)    if not docstring:        return '', '', {}    docstring = cleandoc(docstring)     parts = re.split(r'\n{2,}', docstring)     title = parts[0]     if len(parts) == 1:diff --git a/django/contrib/admindocs/views.py b/django/contrib/admindocs/views.py-- a/django/contrib/admindocs/views.py++ b/django/contrib/admindocs/views.py


import inspect from importlib import import_module<ins>from inspect import cleandoc</ins> from pathlib import Path  from django.apps import apps


continue                 verbose = func.__doc__                 verbose = verbose and (                    utils.parse_rst(utils.trim_docstring(verbose), 'model', _('model:') + opts.model_name)                    utils.parse_rst(cleandoc(verbose), 'model', _('model:') + opts.model_name)                 )                 # Show properties and methods without arguments as fields.                 # Otherwise, show as a 'method with arguments'.


from django.db.models import Q, signals from django.db.models.query import QuerySet from django.db.models.query_utils import DeferredAttribute<ins>from django.db.models.utils import resolve_callables</ins> from django.utils.functional import cached_property


# source_field_name: the PK fieldname in join table for the source object             # target_field_name: the PK fieldname in join table for the target object             # *objs - objects to add. Either object instances, or primary keys of object instances.            through_defaults = through_defaults or {}            # If there aren't any objects, there is nothing to do.            if objs:                target_ids = self._get_target_ids(target_field_name, objs)                db = router.db_for_write(self.through, instance=self.instance)                can_ignore_conflicts, must_send_signals, can_fast_add = self._get_add_plan(db, source_field_name)                if can_fast_add:                    self.through._default_manager.using(db).bulk_create([                        self.through(**{                            '%s_id' % source_field_name: self.related_val[0],                            '%s_id' % target_field_name: target_id,                        })                        for target_id in target_ids                    ], ignore_conflicts=True)                    return            if not objs:                return                 missing_target_ids = self._get_missing_target_ids(                    source_field_name, target_field_name, db, target_ids                )                with transaction.atomic(using=db, savepoint=False):                    if must_send_signals:                        signals.m2m_changed.send(                            sender=self.through, action='pre_add',                            instance=self.instance, reverse=self.reverse,                            model=self.model, pk_set=missing_target_ids, using=db,                        )            through_defaults = dict(resolve_callables(through_defaults or {}))            target_ids = self._get_target_ids(target_field_name, objs)            db = router.db_for_write(self.through, instance=self.instance)            can_ignore_conflicts, must_send_signals, can_fast_add = self._get_add_plan(db, source_field_name)            if can_fast_add:                self.through._default_manager.using(db).bulk_create([                    self.through(**{                        '%s_id' % source_field_name: self.related_val[0],                        '%s_id' % target_field_name: target_id,                    })                    for target_id in target_ids                ], ignore_conflicts=True)                return                     # Add the ones that aren't there already.                    self.through._default_manager.using(db).bulk_create([                        self.through(**through_defaults, **{                            '%s_id' % source_field_name: self.related_val[0],                            '%s_id' % target_field_name: target_id,                        })                        for target_id in missing_target_ids                    ], ignore_conflicts=can_ignore_conflicts)                    if must_send_signals:                        signals.m2m_changed.send(                            sender=self.through, action='post_add',                            instance=self.instance, reverse=self.reverse,                            model=self.model, pk_set=missing_target_ids, using=db,                        )            missing_target_ids = self._get_missing_target_ids(                source_field_name, target_field_name, db, target_ids            )            with transaction.atomic(using=db, savepoint=False):                if must_send_signals:                    signals.m2m_changed.send(                        sender=self.through, action='pre_add',                        instance=self.instance, reverse=self.reverse,                        model=self.model, pk_set=missing_target_ids, using=db,                    )                # Add the ones that aren't there already.                self.through._default_manager.using(db).bulk_create([                    self.through(**through_defaults, **{                        '%s_id' % source_field_name: self.related_val[0],                        '%s_id' % target_field_name: target_id,                    })                    for target_id in missing_target_ids                ], ignore_conflicts=can_ignore_conflicts)                if must_send_signals:                    signals.m2m_changed.send(                        sender=self.through, action='post_add',                        instance=self.instance, reverse=self.reverse,                        model=self.model, pk_set=missing_target_ids, using=db,                    )          def _remove_items(self, source_field_name, target_field_name, *objs):             # source_field_name: the PK colname in join table for the source objectdiff --git a/django/db/models/query.py b/django/db/models/query.py-- a/django/db/models/query.py++ b/django/db/models/query.py


from django.db.models.functions import Cast, Trunc from django.db.models.query_utils import FilteredRelation, Q from django.db.models.sql.constants import CURSOR, GET_ITERATOR_CHUNK_SIZE<ins>from django.db.models.utils import resolve_callables</ins> from django.db.utils import NotSupportedError from django.utils import timezone from django.utils.functional import cached_property, partition


obj, created = self._create_object_from_params(kwargs, params, lock=True)                 if created:                     return obj, created            for k, v in defaults.items():                setattr(obj, k, v() if callable(v) else v)            for k, v in resolve_callables(defaults):                setattr(obj, k, v)             obj.save(using=self.db)         return obj, False


"""         try:             with transaction.atomic(using=self.db):                params = {k: v() if callable(v) else v for k, v in params.items()}                params = dict(resolve_callables(params))                 obj = self.create(**params)             return obj, True         except IntegrityError as e:diff --git a/django/db/models/utils.py b/django/db/models/utils.py-- a/django/db/models/utils.py++ b/django/db/models/utils.py


"Invalid model reference '%s'. String model references "             "must be of the form 'app_label.ModelName'." % model         )<ins>def resolve_callables(mapping):    """    Generate key/value pairs for the given mapping where the values are    evaluated if they're callable.    """    for k, v in mapping.items():        yield k, v() if callable(v) else v</ins>


import asyncio import functools<ins>import os</ins>  from django.core.exceptions import SynchronousOnlyOperation


def decorator(func):         @functools.wraps(func)         def inner(*args, **kwargs):            # Detect a running event loop in this thread.            try:                event_loop = asyncio.get_event_loop()            except RuntimeError:                pass            else:                if event_loop.is_running():                    raise SynchronousOnlyOperation(message)            if not os.environ.get('DJANGO_ALLOW_ASYNC_UNSAFE'):                # Detect a running event loop in this thread.                try:                    event_loop = asyncio.get_event_loop()                except RuntimeError:                    pass                else:                    if event_loop.is_running():                        raise SynchronousOnlyOperation(message)             # Pass onwards.             return func(*args, **kwargs)         return inner


# If there are any named groups, use those as kwargs, ignoring             # non-named groups. Otherwise, pass all non-named arguments as             # positional arguments.            kwargs = {k: v for k, v in match.groupdict().items() if v is not None}            kwargs = match.groupdict()             args = () if kwargs else match.groups()            kwargs = {k: v for k, v in kwargs.items() if v is not None}             return path[match.end():], args, kwargs         return None


def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):         # We must promote any new joins to left outer joins so that when Q is         # used as an expression, rows aren't filtered due to joins.        clause, joins = query._add_q(self, reuse, allow_joins=allow_joins, split_subq=False)        clause, joins = query._add_q(            self, reuse, allow_joins=allow_joins, split_subq=False,            check_filterable=False,        )         query.promote_joins(joins)         return clause diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py-- a/django/db/models/sql/query.py++ b/django/db/models/sql/query.py


def check_filterable(self, expression):         """Raise an error if expression cannot be used in a WHERE clause."""        if not getattr(expression, 'filterable', 'True'):        if not getattr(expression, 'filterable', True):             raise NotSupportedError(                 expression.__class__.__name__ + ' is disallowed in the filter '                 'clause.'


def build_filter(self, filter_expr, branch_negated=False, current_negated=False,                      can_reuse=None, allow_joins=True, split_subq=True,                     reuse_with_filtered_relation=False):                     reuse_with_filtered_relation=False, check_filterable=True):         """         Build a WhereNode for a single filter clause but don't add it         to this Query. Query.add_q() will then add this filter to the where


used_aliases=can_reuse,                 allow_joins=allow_joins,                 split_subq=split_subq,<ins>                check_filterable=check_filterable,</ins>             )         if hasattr(filter_expr, 'resolve_expression'):             if not getattr(filter_expr, 'conditional', False):


raise FieldError("Cannot parse keyword query %r" % arg)         lookups, parts, reffed_expression = self.solve_lookup_type(arg)         self.check_filterable(reffed_expression)        if check_filterable:            self.check_filterable(reffed_expression)          if not allow_joins and len(parts) > 1:             raise FieldError("Joined field references are not permitted in this query")


value = self.resolve_lookup_value(value, can_reuse, allow_joins)         used_joins = {k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0)}         self.check_filterable(value)        if check_filterable:            self.check_filterable(value)          clause = self.where_class()         if reffed_expression: