Question: from django.core.exceptions import FieldError, ValidationError from django.db import connectionsfrom django.db.models.expressions import Exists, ExpressionList, Ffrom django.db.models.expressions import Exists, ExpressionList, F, OrderBy from django.db.models.indexes import IndexExpression from django.db.models.lookups import Exact from django.db.models.query_utils import Q

Validation of UniqueConstraint with ordered expressions crashes. Description I this model: class Foo(models.Model): name = models.CharField(maxlength=300, unique=True) class Meta: constraints = [ UniqueConstraint( Lower("name").desc(), name="uniquelowername" ) ] and this admin class: class FooAdmin(admin.ModelAdmin): searchfields = ("name",) I based the UniqueConstraint on the documentation (), which shows UniqueConstraint(Lower('name').desc(), 'category', name='uniquelowernamecategory') When I visit the admin and click "add foo", enter name "foo1", and click Save, I get a stack trace: Request Method: POST Request URL: http://localhost:8000/admin/myapp/foo/add/ Django Version: 4.1.5 Python Version: 3.10.9 ... Traceback (most recent call last): File ".../lib/python3.10/site-", line 89, in execute return (sql, params) The above exception (syntax error at or near "DESC" LINE 1: ...werappfoo" WHERE LOWER("myappfoo"."name") DESC = (LO. ^ ) was the direct cause of the following exception: File "./lib/python3.10/site-", line 55, in inner response = getresponse(request) File "./lib/python3.10/site-", line 197, in getresponse response = wrappedcallback(request, callbackargs, callbackkwargs) File "./lib/python3.10/site-", line 686, in wrapper return self.adminsite.adminview(view)(args, kwargs) File "./lib/python3.10/site-", line 133, in wrappedview response = viewfunc(request, args, kwargs) File "./lib/python3.10/site-", line 62, in wrappedviewfunc response = viewfunc(request, args, kwargs) File "./lib/python3.10/site-", line 242, in inner return view(request, args, kwargs) File "./lib/python3.10/site-", line 1890, in addview return self.changeformview(request, None, formurl, extracontext) File "./lib/python3.10/site-", line 46, in wrapper return boundmethod(args, kwargs) File "./lib/python3.10/site-", line 133, in wrappedview response = viewfunc(request, args, *kwargs) File "./lib/python3.10/site-", line 1750,


Question: from django.core.exceptions import FieldError, ValidationError from django.db import connectionsfrom django.db.models.expressions import Exists, ExpressionList, Ffrom django.db.models.expressions import Exists, ExpressionList, F, OrderBy from django.db.models.indexes import IndexExpression from django.db.models.lookups import Exact from django.db.models.query_utils import Q

in changeformview return self.changeformview(request, objectid, formurl, extracontext) File "./lib/python3.10/site-", line 1796, in changeformview formvalidated = form.isvalid() File "./lib/python3.10/site-", line 205, in isvalid return self.isbound and not File "./lib/python3.10/site-", line 200, in errors self.fullclean() File "./lib/python3.10/site-", line 439, in fullclean self.postclean() File "./lib/python3.10/site-", line 492, in postclean self.instance.fullclean(exclude=exclude, validateunique=False) File "./lib/python3.10/site-", line 1472, in fullclean self.validateconstraints(exclude=exclude) File "./lib/python3.10/site-", line 1423, in validateconstraints constraint.validate(modelclass, self, exclude=exclude, using=using) File "./lib/python3.10/site-", line 347, in validate if (): File "./lib/python3.10/site-", line 1226, in exists return () File "./lib/python3.10/site-", line 592, in hasresults return compiler.hasresults() File "./lib/python3.10/site-", line 1366, in hasresults return bool(self.executesql(SINGLE)) File "./lib/python3.10/site-", line 1398, in executesql cursor.execute(sql, params) File "./lib/python3.10/site-", line 230, in execute return (, sql, params) File "./lib/python3.10/site-", line 154, in record return method(sql, params) File "./lib/python3.10/site-", line 103, in execute return super().execute(sql, params) File "./lib/python3.10/site-", line 67, in execute return self.executewithwrappers( File "./lib/python3.10/site-", line 80, in executewithwrappers return executor(sql, params, many, context) File "./lib/python3.10/site-", line 84, in execute with File "./lib/python3.10/site-", line 91, in exit raise djexcvalue.withtraceback(traceback) from excvalue File "./lib/python3.10/site-", line 89, in execute return (sql,


Question: from django.core.exceptions import FieldError, ValidationError from django.db import connectionsfrom django.db.models.expressions import Exists, ExpressionList, Ffrom django.db.models.expressions import Exists, ExpressionList, F, OrderBy from django.db.models.indexes import IndexExpression from django.db.models.lookups import Exact from django.db.models.query_utils import Q

params) Exception Type: ProgrammingError at /admin/myapp/foo/add/ Exception Value: syntax error at or near "DESC" LINE 1: .myappfoo" WHERE LOWER("myappfoo"."name") DESC = (LO... ^ This happens on the Postgres backend (psycopg2 2.9.5). I also get this error using the sqlite backend. Exception Type: OperationalError at /admin/myapp/foo/add/ Exception Value: near "DESC": syntax error


Question: meta=model._meta, exclude=exclude                 ).items()             }            expressions = [                Exact(expr, expr.replace_expressions(replacements))                for expr in self.expressions            ]            expressions = []            for expr in self.expressions:                # Ignore ordering.                if isinstance(expr, OrderBy):                    expr = expr.expression                expressions.append(Exact(expr, expr.replace_expressions(replacements)))             queryset = queryset.filter(*expressions)         model_class_pk = instance._get_pk_val(model._meta)         if not instance._state.adding and model_class_pk is not None:


Question: meta=model._meta, exclude=exclude                 ).items()             }            expressions = [                Exact(expr, expr.replace_expressions(replacements))                for expr in self.expressions            ]            expressions = []            for expr in self.expressions:                # Ignore ordering.                if isinstance(expr, OrderBy):                    expr = expr.expression                expressions.append(Exact(expr, expr.replace_expressions(replacements)))             queryset = queryset.filter(*expressions)         model_class_pk = instance._get_pk_val(model._meta)         if not instance._state.adding and model_class_pk is not None:


Question: meta=model._meta, exclude=exclude                 ).items()             }            expressions = [                Exact(expr, expr.replace_expressions(replacements))                for expr in self.expressions            ]            expressions = []            for expr in self.expressions:                # Ignore ordering.                if isinstance(expr, OrderBy):                    expr = expr.expression                expressions.append(Exact(expr, expr.replace_expressions(replacements)))             queryset = queryset.filter(*expressions)         model_class_pk = instance._get_pk_val(model._meta)         if not instance._state.adding and model_class_pk is not None:


Question: import socket import socketserver import sys<ins>from collections import deque</ins> from wsgiref import simple_server  from django.core.exceptions import ImproperlyConfigured

After runserver returns response body for HTTP HEAD requests Description For compliance with RFC 2616, section 4.3, response bodies must not be returned for HEAD requests. In , the stripping of the response bodies was removed from Django in favour of letting the server perform the body removal, since the common servers (gunicorn, mod_wsgi etc) already do so. However it appears that runserver does not strip the body, contrary to: As such, starting in Django 1.10 the responses from runserver for HEAD requests are no longer compliant with the spec. (In certain configurations this also results in "Broken pipe" error messages in runserver output, since compliant user agents expect to be able to terminate the connection after the headers are sent.) STR: 1) mkvirtualenv django-test 2) pip install 'Django1.10,<1.11' 3) django-admin startproject django-test 4) cd django-test 5) runserver 6) In another terminal, run curl -iX HEAD http://127.0.0.1:8000/ 7) Observe response from curl Expected: HTTP/1.0 200 OK Date: Fri, 07 Apr 2017 14:56:39 GMT Server: WSGIServer/0.2 CPython/3.4.5 Content-Type: text/html X-Frame-Options: SAMEORIGIN Actual: HTTP/1.0 200 OK Date: Fri, 07 Apr 2017 14:56:39 GMT Server: WSGIServer/0.2 CPython/3.4.5 Content-Type: text/html X-Frame-Options: SAMEORIGIN <!DOCTYPE html<html lang="en"<head<meta http-equiv="content-type" content="text/html; charset=utf-8"<meta name="robots" content="NONE,NOARCHIVE"<titleWelcome to Django</title... Tested with Python 2.7.13 and 3.4.5. Doesn't reproduce under Django 1.9.13.


Question: def cleanup_headers(self):         super().cleanup_headers()        if (            self.environ["REQUEST_METHOD"] == "HEAD"            and "Content-Length" in self.headers        ):            del self.headers["Content-Length"]         # HTTP/1.1 requires support for persistent connections. Send 'close' if         # the content length is unknown to prevent clients from reusing the         # connection.        if "Content-Length" not in self.headers:        if (            self.environ["REQUEST_METHOD"] != "HEAD"            and "Content-Length" not in self.headers        ):             self.headers["Connection"] = "close"         # Persistent connections require threading server.         elif not isinstance(self.request_handler.server, socketserver.ThreadingMixIn):


Question: self.get_stdin().read()         super().close() <ins>    def finish_response(self):        if self.environ["REQUEST_METHOD"] == "HEAD":            try:                deque(self.result, maxlen=0)  # Consume iterator.                # Don't call self.finish_content() as, if the headers have not                # been sent and Content-Length isn't set, it'll default to "0"                # which will prevent omission of the Content-Length header with                # HEAD requests as permitted by RFC 9110 Section 9.3.2.                # Instead, send the headers, if not sent yet.                if not self.headers_sent:                    self.send_headers()            finally:                self.close()        else:            super().finish_response()</ins>  class WSGIRequestHandler(simple_server.WSGIRequestHandler):     protocol_version = "HTTP/1.1"


Question: **kwargs,         )     def update_or_create(self, defaults=None, **kwargs):    def update_or_create(self, defaults=None, create_defaults=None, **kwargs):         """         Look up an object with the given kwargs, updating one with defaults        if it exists, otherwise create a new one.        if it exists, otherwise create a new one. Optionally, an object can        be created with different values than defaults by using        create_defaults.         Return a tuple (object, created), where created is a boolean         specifying whether an object was created.         """        defaults = defaults or {}        if create_defaults is None:            update_defaults = create_defaults = defaults or {}        else:            update_defaults = defaults or {}         self._for_write = True         with transaction.atomic(using=self.db):             # Lock the row so that a concurrent update is blocked until             # update_or_create() has performed its save.            obj, created = self.select_for_update().get_or_create(defaults, **kwargs)            obj, created = self.select_for_update().get_or_create(                create_defaults, **kwargs            )             if created:                 return obj, created            for k, v in resolve_callables(defaults):            for k, v in resolve_callables(update_defaults):                 setattr(obj, k, v)             update_fields = set(defaults)            update_fields = set(update_defaults)             concrete_field_names = self.model._meta._non_pk_concrete_field_names             # update_fields does not support non-concrete fields.             if concrete_field_names.issuperset(update_fields):

Support create defaults for updateorcreate Description I proposed the idea of extending updateorcreate to support specifying a different set of defaults for the create operation on the . There seems to be consensus it's a positive add to Django. Adam raised concerns with my proposed approach of adding a createdefaults parameter to the function since this would conflict with any fields on a model named, createdefaults. Jeff did a code search on github for that term and didn't find any matches. I suspect if someone where using a field named createdefaults, it would be a JSON or object type field. Those don't seem like reasonable candidates to be part of a UniqueConstraint, which should be underlying the look-up arguments to updateorcreate. I do like the idea of having a separate parameter for createdefaults, but if we must preserve 100% backwards compatibility, Adam's suggestion of having defaults be set to another object makes the most sense. My blocking question is, which approach should I take? From the forum post: I’ve run into a use-case in which it’d be helpful to have the ability to specify a different set of defaults for the update operation compared to the create operation. While I don’t expect my particular use case to translate, here’s a more generic one. Given the following Record model: class Record(models.Model): someid = models.CharField(unique=True) createdby = models.ForeignKey(User, ...) modifiedby = models.ForeignKey(User, null=True, blank=True, ...) When a record is created, we would want to set createdby, but if it’s being updated, we’d want to set modifiedby. This use case can’t be solved by using updateorcreate, unless it allows for us to specify a different set of default values. Record.objects.updateorcreate( someid=somevalue, defaults={"modifiedby": user}, createdefaults={"createdby": user}, )


Question: obj.save(using=self.db)         return obj, False     async def aupdate_or_create(self, defaults=None, **kwargs):    async def aupdate_or_create(self, defaults=None, create_defaults=None, **kwargs):         return await sync_to_async(self.update_or_create)(             defaults=defaults,            create_defaults=create_defaults,             **kwargs,         )


Question: "available_apps": self.get_app_list(request),             "is_popup": False,             "is_nav_sidebar_enabled": self.enable_nav_sidebar,<ins>            "log_entries": self.get_log_entries(request),</ins>         }      def password_change(self, request, extra_context=None):

Admin Site shall filter LogEntries by registered models Description This ticket has been discussed here:  If an implementer registers more than one AdminSite, one might expect that only changes on models registered at that specific site are logged. This currently is not the case, all registered sites show all entries of LogEntry. It is confusing for users to access a specific site and see log entries for models they can't access.


Question: context,         )     def get_log_entries(self, request):        from django.contrib.admin.models import LogEntry        return LogEntry.objects.select_related("content_type", "user")  class DefaultAdminSite(LazyObject):     def _setup(self):diff --git a/django/contrib/admin/templatetags/log.py b/django/contrib/admin/templatetags/log.py-- a/django/contrib/admin/templatetags/log.py++ b/django/contrib/admin/templatetags/log.py


Question: from django import template<del>from django.contrib.admin.models import LogEntry</del>  register = template.Library()


Question: return "<GetAdminLog Node>"      def render(self, context):        if self.user is None:            entries = LogEntry.objects.all()        else:        entries = context["log_entries"]        if self.user is not None:             user_id = self.user             if not user_id.isdigit():                 user_id = context[self.user].pk            entries = LogEntry.objects.filter(user__pk=user_id)        context[self.varname] = entries.select_related("content_type", "user")[            : int(self.limit)        ]            entries = entries.filter(user__pk=user_id)        context[self.varname] = entries[: int(self.limit)]         return ""


Question: def create_reference_role(rolename, urlbase):<ins>    # Views and template names are case-sensitive.    is_case_sensitive = rolename in ["template", "view"]</ins>     def _role(name, rawtext, text, lineno, inliner, options=None, content=None):         if options is None:             options = {}

Mixed-case views/templates names causes 404 on :view:/:template: directive. Description  Using a class based view, class OrderSearch(LoginRequiredMixin, UserPassesTestMixin, ListView): add a doc comment such as causes a 404 when you click on the link in the docs Page not found (404) Request Method: GET Request URL:  Raised by: I'm not sure exactly where orders becomes lowercase, but I thought it might have something to do with the getview_func


Question: urlbase                 % (                     inliner.document.settings.link_base,                    text.lower(),                    text if is_case_sensitive else text.lower(),                 )             ),             **options,


Question: and context.get("show_delete", True)             ),             "show_save_as_new": not is_popup            and has_change_permission            and has_add_permission             and change             and save_as,             "show_save_and_add_another": can_save_and_add_another,

"showsaveasnew" in admin can add without this permission Description (last modified by Mariusz Felisiak) At "" file, line 102, I think you must put one more verification for this tag: "and hasaddpermission", because "saveasnew" is a add modification. I rewrite this for my project: "showsaveasnew": not ispopup and hasaddpermission # This line that I put!!! and haschangepermission and change and save_as,


Question: or not new_field.remote_field.through._meta.auto_created                 ):                     continue                # Rename the M2M table that's based on this model's name.                old_m2m_model = old_field.remote_field.through                new_m2m_model = new_field.remote_field.through                schema_editor.alter_db_table(                    new_m2m_model,                    old_m2m_model._meta.db_table,                    new_m2m_model._meta.db_table,                )                # Rename the column in the M2M table that's based on this                # model's name.                schema_editor.alter_field(                    new_m2m_model,                    old_m2m_model._meta.get_field(old_model._meta.model_name),                    new_m2m_model._meta.get_field(new_model._meta.model_name),                # Rename columns and the M2M table.                schema_editor._alter_many_to_many(                    new_model,                    old_field,                    new_field,                    strict=False,                 )      def database_backwards(self, app_label, schema_editor, from_state, to_state):

Duplicate model names in M2M relationship causes RenameModel migration failure Description Example code is here:  I have a django project with two apps, incidents and vault, that both have a model named Incident. The vault Incident model has an M2M involving the incidents Incident model. When the table is created for this M2M relationship the automatic field names are "fromincidentid" and "toincidentid" since models have the same names. If I then try to use a RenameModel in a migration. operations = [ migrations.RenameModel( oldname='Incident', newname='Folder', ), ] it fails with this traceback: Tracking file by folder pattern: migrations Operations to perform: Apply all migrations: admin, auth, contenttypes, incidents, sessions, vault Running migrations: Applying vault.0002renameincidentfolder.Traceback (most recent call last): File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 668, in getfield return self.fieldsmap[fieldname] KeyError: 'incident' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/zacmiller/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-", line 52, in <moduleruncommand() File "/Users/zacmiller/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-", line 46, in runcommand runmodule(managefile, None, 'main', True) File "", line 209, in runmodule return runmodulecode(code, initglobals, runname, modspec) File "", line 96, in runmodulecode runcode(code, modglobals, initglobals, File "", line 86, in runcode exec(code, runglobals) File "", line 22, in <modulemain() File "", line 18, in main executefromcommandline() File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 446, in executefromcommandline utility.execute() File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 440, in execute self.fetchcommand(subcommand).


Question: or not new_field.remote_field.through._meta.auto_created                 ):                     continue                # Rename the M2M table that's based on this model's name.                old_m2m_model = old_field.remote_field.through                new_m2m_model = new_field.remote_field.through                schema_editor.alter_db_table(                    new_m2m_model,                    old_m2m_model._meta.db_table,                    new_m2m_model._meta.db_table,                )                # Rename the column in the M2M table that's based on this                # model's name.                schema_editor.alter_field(                    new_m2m_model,                    old_m2m_model._meta.get_field(old_model._meta.model_name),                    new_m2m_model._meta.get_field(new_model._meta.model_name),                # Rename columns and the M2M table.                schema_editor._alter_many_to_many(                    new_model,                    old_field,                    new_field,                    strict=False,                 )      def database_backwards(self, app_label, schema_editor, from_state, to_state):

runfromargv() File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 402, in runfromargv self.execute(args, cmdoptions) File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 448, in execute output = (args, options) File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 96, in wrapped res = handlefunc(args, *kwargs) File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 349, in handle postmigratestate = executor.migrate( File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 135, in migrate state = self.migrateallforwards( File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 167, in migrateallforwards state = self.applymigration( File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 252, in applymigration state = (state, schemaeditor) File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 130, in apply operation.databaseforwards( File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 422, in databaseforwards (), File "/Users/zacmiller/PycharmProjects/virtualenvs/edemo/lib/python3.10/site-", line 670, in getfield raise FieldDoesNotExist( Incidentincidents has no field named 'incident'


Question: required=False,                     widget=self.get_ordering_widget(),                 )        if self.can_delete and (self.can_delete_extra or index < initial_form_count):        if self.can_delete and (            self.can_delete_extra or (index is not None and index < initial_form_count)        ):             form.fields[DELETION_FIELD_NAME] = BooleanField(                 label=_("Delete"),                 required=False,

Formsets' addfields() method fails in some circumstances if the argument index is None. Description Formsets' addfields() method fails in some circumstances if the argument index is None. When a FormSet has the attributes self.candelete == True and self.candeleteextra == False, calling the addfields() method on that FormSet fails if the argument index is None. This occurs for example when calling FormSet.emptyform(). The result is that the method raises the exception TypeError: '<' not supported between instances of 'NoneType' and 'int'. Code example: MyFormSet = forms.formsetfactory( form=MyForm, candelete=True, candeleteextra=False, ) myformset = MyFormSet( initial=None, ) print(myformset.emptyform) The reason this happens is that in in line 493 of index is compared to initialformcount: if self.candelete and (self.candeleteextra or index < initialformcount): Checking for index not None should fix the issue: if self.candelete and (self.candeleteextra or (index is not None and index < initialformcount)): How to Reproduce A self-contained example to reproduce this bug is as follows: #!/usr/bin/env python3 import os import django from django import forms class MyForm(forms.Form): myfield = forms.CharField() if name == "main": settingsfile = ((file))[0] ( DEBUG=True, MIDDLEWARECLASSES=[], ROOTURLCONF=settingsfile, ) () MyFormSet = forms.formsetfactory( form=MyForm, candelete=True, candeleteextra=False, ) myformset = MyFormSet( initial=None, ) print(myformset.emptyform)


Question: <ins>import warnings</ins> from enum import Enum from types import NoneType

Make BaseConstraint arguments keyword-only Description As suggested in  I think we should change the signature of BaseConstraint to use keyword-only arguments as a separate cleanup name and violationerrormessage are already keyword-only in all the BaseConstraint subclasses in Django code base.


Question: from django.db.models.query_utils import Q from django.db.models.sql.query import Query from django.db.utils import DEFAULT_DB_ALIAS<ins>from django.utils.deprecation import RemovedInDjango60Warning</ins> from django.utils.translation import gettext_lazy as _  __all__ = ["BaseConstraint", "CheckConstraint", "Deferrable", "UniqueConstraint"]


Question: default_violation_error_message = _("Constraint “%(name)s” is violated.")     violation_error_message = None     def __init__(self, name, violation_error_message=None):    # RemovedInDjango60Warning: When the deprecation ends, replace with:    # def __init__(self, *, name, violation_error_message=None):    def __init__(self, *args, name=None, violation_error_message=None):        # RemovedInDjango60Warning.        if name is None and not args:            raise TypeError(                f"{self.__class__.__name__}.__init__() missing 1 required keyword-only "                f"argument: 'name'"            )         self.name = name         if violation_error_message is not None:             self.violation_error_message = violation_error_message         else:             self.violation_error_message = self.default_violation_error_message        # RemovedInDjango60Warning.        if args:            warnings.warn(                f"Passing positional arguments to {self.__class__.__name__} is "                f"deprecated.",                RemovedInDjango60Warning,                stacklevel=2,            )            for arg, attr in zip(args, ["name", "violation_error_message"]):                if arg:                    setattr(self, attr, arg)      @property     def contains_expressions(self):


Question: raise TypeError(                 "CheckConstraint.check must be a Q instance or boolean expression."             )        super().__init__(name, violation_error_message=violation_error_message)        super().__init__(name=name, violation_error_message=violation_error_message)      def _get_check_sql(self, model, schema_editor):         query = Query(model=model, alias_cols=False)


Question: F(expression) if isinstance(expression, str) else expression             for expression in expressions         )        super().__init__(name, violation_error_message=violation_error_message)        super().__init__(name=name, violation_error_message=violation_error_message)      @property     def contains_expressions(self):


Question: import re import types import warningsfrom decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperationfrom decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation, getcontext from functools import wraps from inspect import unwrap from operator import itemgetter

floatformat() crashes on "0.0000" Description Similar to the current Django code (both 4.2b1 and latest git) crash on using the floatformat template filter with 0 values. from decimal import Decimal from django.template.defaultfilters import floatformat floatformat('0.0000', 2) floatformat(Decimal('0.0000'), 2) Both throw ValueError: valid range for prec is [1, MAXPREC] Using git bisect I tracked the bug to commit . When one uses "0.0000": floatformat:2 the current code results in a precision of 0 which is not allowed: tupl = d.astuple() # with d being "0.0000" returns (sign=0,digits=(0,),exponent=-4) units = len(tupl[1]) # equals 1 units += -tupl[2] if m else tupl[2] # 1 + (-4) prec = abs(p) + units + 1 # 2 (number of requested decimals) + (-3) + 1 equals 0 roundedd = d.quantize(exp, ROUNDHALF_UP, Context(prec=prec))


Question: units = len(tupl[1])     units += -tupl[2] if m else tupl[2]     prec = abs(p) + units + 1<ins>    prec = max(getcontext().prec, prec)</ins>      # Avoid conversion to scientific notation by accessing `sign`, `digits`,     # and `exponent` from Decimal.as_tuple() directly.


Question: return "alter_%s_%s" % (self.model_name_lower, self.name_lower)      def reduce(self, operation, app_label):        if isinstance(operation, RemoveField) and self.is_same_field_operation(            operation        ):        if isinstance(            operation, (AlterField, RemoveField)        ) and self.is_same_field_operation(operation):             return [operation]         elif (             isinstance(operation, RenameField)

Migration optimizer does not reduce multiple AlterField Description Let's consider the following operations: operations = [ migrations.AddField( modelname="book", name="title", field=models.CharField(maxlength=256, null=True), ), migrations.AlterField( modelname="book", name="title", field=models.CharField(maxlength=128, null=True), ), migrations.AlterField( modelname="book", name="title", field=models.CharField(maxlength=128, null=True, helptext="help"), ), migrations.AlterField( modelname="book", name="title", field=models.CharField(maxlength=128, null=True, helptext="help", default=None), ), ] If I run the optimizer, I get only the AddField, as we could expect. However, if the AddField model is separated from the AlterField (e.g. because of a non-elidable migration, or inside a non-squashed migration), none of the AlterField are reduced: optimizer.optimize(operations[1:], "books") [<AlterField modelname='book', name='title', field=, <AlterField modelname='book', name='title', field=, <AlterField modelname='book', name='title', field=] Indeed, the does not consider the the case where operation is also an AlterField. Is this behaviour intended? If so, could it be documented? Otherwise, would it make sense to add something like if isinstance(operation, AlterField) and self.issamefieldoperation( operation ): return [operation]


Question: def resolve_expression(self, *args, **kwargs):         col = super().resolve_expression(*args, **kwargs)        if col.contains_over_clause:            raise NotSupportedError(                f"Referencing outer query window expression is not supported: "                f"{self.name}."            )         # FIXME: Rename possibly_multivalued to multivalued and fix detection         # for non-multivalued JOINs (e.g. foreign key fields). This should take         # into account only many-to-many and one-to-many relationships.diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py-- a/django/db/models/sql/compiler.py++ b/django/db/models/sql/compiler.py

Subquery referencing WINDOW expression breaks query compilation Description introduced the ability to filter on WINDOW expressions but when a WINDOW expression is referred to in a subquery, the query compiler fails: ValueError: Need either using or connection I assume it has to do with self.connection not being passed on to innerquery.getcompiler in getqualifysql introduced here:  Below is the full traceback. A dummy test is available here:  Traceback (most recent call last): File "", line 57, in testPartExecutor yield File "", line 623, in run self.callTestMethod(testMethod) File "", line 579, in callTestMethod if method() is not None: File "", line 1025, in testfiltersubquery list( File "", line 398, in iter self.fetchall() File "", line 1881, in fetchall self.resultcache = list(self.iterableclass(self)) File "", line 91, in iter results = compiler.executesql( File "", line 1545, in executesql sql, params = () File "", line 732, in assql extraselect, orderby, groupby = self.presqlsetup( File "", line 84, in presqlsetup self.setupquery(withcolaliases=withcolaliases) File "", line 73, in setupquery , self.klassinfo, self.annotationcolmap = self.getselect( File "", line 296, in getselect sql, params = self.compile(col) File "", line 542, in compile sql, params = (self, self.connection) File "", line 1536, in assql subquerysql, sqlparams = (compiler, connection) File "", line 1150, in assql sql, params = self.getcompiler(connection=connection).assql() File "", line 751, in assql result, params = self.getqualifysql() File "", line 676, in getqualifysql innerquerycompiler = innerquery.getcompiler( File "", line 298, in get_compiler raise ValueError("Need either using or connection") ValueError: Need either using or connection


Question: )             )         inner_query_compiler = inner_query.get_compiler(            self.using, elide_empty=self.elide_empty            self.using, connection=self.connection, elide_empty=self.elide_empty         )         inner_sql, inner_params = inner_query_compiler.as_sql(             # The limits must be applied to the outer query to avoid pruning


Question: import itertools import math from django.core.exceptions import EmptyResultSetfrom django.core.exceptions import EmptyResultSet, FullResultSet from django.db.models.expressions import Case, Expression, Func, Value, When from django.db.models.fields import (     BooleanField,

Querying with an integer larger than SQLite supports crashes with OverflowError Description Hi, i don't know, you accept this behavior Overflow on SQLite or there is max length here? Step : http://127.0.0.1:8000/admin/auth/user//change/ Error : OverflowError at /admin/auth/user//change/ Python int too large to convert to SQLite INTEGER Request Method: GET Request URL: http://127.0.0.1:8000/admin/auth/user//change/ Django Version: 1.10.2 Exception Type: OverflowError Exception Value: Python int too large to convert to SQLite INTEGER Exception Location: C:\Python27\lib\site-packages\django\db\backends\sqlite3\ in quoteparamsforlastexecutedquery, line 129 Python Executable: C:\Python27\ Python Version: 2.7.11 Python Path: ['E:/Programmer Language/NodeJS/untitled1', 'C:\Users\RaminFP\.IntelliJIdea14\config\plugins\python\helpers\pydev', 'C:\Python27\lib\site-packages\six-1.10.0-', 'C:\Python27\lib\site-packages\pisa-3.0.33-', 'C:\Python27\lib\site-packages\sqlacodegen-1.1.6-', 'C:\Python27\lib\site-packages\inflect-0.2.5-', 'C:\Python27\lib\site-packages\0x10c_asm-0.0.2-', 'C:\Python27\lib\site-packages\jpype1-0.6.1-py2.7-', 'C:\Python27\lib\site-packages\simpleaes-1.0-', 'C:\Python27\lib\site-packages\celery-3.1.23-', 'C:\Python27\lib\site-packages\kombu-3.0.35-', 'C:\Python27\lib\site-packages\anyjson-0.3.3-', 'C:\Python27\lib\site-packages\pythonnet-2.1.0.dev1-py2.7-', 'E:\Programmer Language\NodeJS\untitled1', 'C:\Windows\SYSTEM32\', 'C:\Python27\DLLs', 'C:\Python27\lib',


Question: import itertools import math from django.core.exceptions import EmptyResultSetfrom django.core.exceptions import EmptyResultSet, FullResultSet from django.db.models.expressions import Case, Expression, Func, Value, When from django.db.models.fields import (     BooleanField,

'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages', 'c:\python27\lib\site-packages'] Server time: Fri, 28 Oct 2016 11:43:05 +0000


Question: lookup_name = "lte"  <ins>class IntegerFieldOverflow:    underflow_exception = EmptyResultSet    overflow_exception = EmptyResultSet    def process_rhs(self, compiler, connection):        rhs = self.rhs        if isinstance(rhs, int):            field_internal_type = self.lhs.output_field.get_internal_type()            min_value, max_value = connection.ops.integer_field_range(                field_internal_type            )            if min_value is not None and rhs < min_value:                raise self.underflow_exception            if max_value is not None and rhs > max_value:                raise self.overflow_exception        return super().process_rhs(compiler, connection)</ins> class IntegerFieldFloatRounding:     """     Allow floats to work as query values for IntegerField. Without this, the


Question: lookup_name = "lte"  <ins>class IntegerFieldOverflow:    underflow_exception = EmptyResultSet    overflow_exception = EmptyResultSet    def process_rhs(self, compiler, connection):        rhs = self.rhs        if isinstance(rhs, int):            field_internal_type = self.lhs.output_field.get_internal_type()            min_value, max_value = connection.ops.integer_field_range(                field_internal_type            )            if min_value is not None and rhs < min_value:                raise self.underflow_exception            if max_value is not None and rhs > max_value:                raise self.overflow_exception        return super().process_rhs(compiler, connection)</ins> class IntegerFieldFloatRounding:     """     Allow floats to work as query values for IntegerField. Without this, the


Question: @IntegerField.register_lookupclass IntegerGreaterThanOrEqual(IntegerFieldFloatRounding, GreaterThanOrEqual):class IntegerFieldExact(IntegerFieldOverflow, Exact):     pass   @IntegerField.register_lookupclass IntegerLessThan(IntegerFieldFloatRounding, LessThan):    passclass IntegerGreaterThan(IntegerFieldOverflow, GreaterThan):    underflow_exception = FullResultSet@IntegerField.register_lookupclass IntegerGreaterThanOrEqual(    IntegerFieldOverflow, IntegerFieldFloatRounding, GreaterThanOrEqual):    underflow_exception = FullResultSet@IntegerField.register_lookupclass IntegerLessThan(IntegerFieldOverflow, IntegerFieldFloatRounding, LessThan):    overflow_exception = FullResultSet@IntegerField.register_lookupclass IntegerLessThanOrEqual(IntegerFieldOverflow, LessThanOrEqual):    overflow_exception = FullResultSet   @Field.register_lookup


Question: @IntegerField.register_lookupclass IntegerGreaterThanOrEqual(IntegerFieldFloatRounding, GreaterThanOrEqual):class IntegerFieldExact(IntegerFieldOverflow, Exact):     pass   @IntegerField.register_lookupclass IntegerLessThan(IntegerFieldFloatRounding, LessThan):    passclass IntegerGreaterThan(IntegerFieldOverflow, GreaterThan):    underflow_exception = FullResultSet@IntegerField.register_lookupclass IntegerGreaterThanOrEqual(    IntegerFieldOverflow, IntegerFieldFloatRounding, GreaterThanOrEqual):    underflow_exception = FullResultSet@IntegerField.register_lookupclass IntegerLessThan(IntegerFieldOverflow, IntegerFieldFloatRounding, LessThan):    overflow_exception = FullResultSet@IntegerField.register_lookupclass IntegerLessThanOrEqual(IntegerFieldOverflow, LessThanOrEqual):    overflow_exception = FullResultSet   @Field.register_lookup


Question: if (                         field.nulls_first is None and field.nulls_last is None                     ) or self.connection.features.supports_order_by_nulls_modifier:                        field = field.copy()                         field.expression = select_ref                     # Alias collisions are not possible when dealing with                     # combined queries so fallback to it if emulation of NULLS                     # handling is required.                     elif self.query.combinator:                        field = field.copy()                         field.expression = Ref(select_ref.refs, select_ref.source)                 yield field, select_ref is not None                 continue

Field position reference for aggregate ends up in group-by clause Description Changeset introduced a regression. Aggregate queries are rejected by the database due to the aggregated field being to the GROUP BY clause. It was difficult for me to pin down, especially because it looks like the error only occurs on the second evaluation of the query. The first query is executed just fine and doesn't contain the position reference to the aggregated field in the GROUP BY, only the second one. Below is a test to reproduce the behaviour: diff --git index .. a b class AggregationTests(TestCase): lambda b: (b.name, b.authorCount), ) 1324 def testquotingaggregateorderbyf(self): 1325 author = (name="Peter Norvig") 1326 qs = ( 1327 () 1328 .annotate(num=Count("authors")) 1329 .orderby(F("num").desc()) 1330 ) 1331 list(qs.iterator()) 1332 list(qs.iterator()) 1333 def test_stddev(self): self.assertEqual( Book.objects.aggregate(StdDev("pages")),


Question: <ins>import asyncio</ins> import logging import sys import tempfile

ASGI http.disconnect not handled on requests with body. Description Noticed whilst reviewing PR for , we're not handling the ASGI http.disconnect message correctly. Since it's only dealt with whilst reading the request body, http.disconnect is not processed on a request that includes a body.  async def readbody(self, receive): """Reads an HTTP body from an ASGI connection.""" # Use the tempfile that auto rolls-over to a disk file as it fills up. bodyfile = tempfile.SpooledTemporaryFile( maxsize=settings.FILEUPLOADMAXMEMORYSIZE, mode="w+b" ) while True: message = await receive() if message["type"] == "http.disconnect": # This is the only place is handled. () # Early client disconnect. raise RequestAborted() # Add a body chunk from the message, if provided. if "body" in message: (message["body"]) # Quit out if that's the end. if not ("morebody", False): break (0) return bodyfile http.disconnect is designed for long-polling — so we imagine a client opening a request, with a request body, and then disconnecting before the response is generated. The protocol server (Daphne/uvicorn/...) will send the http.diconnect message, but it's not handled. This test fails on main (at — 4.2 pre-alpha) diff --git index .. --- +++ -188,6 +188,18 class ASGITest(SimpleTestCase): with self.assertRaises(asyncio.TimeoutError): await communicator.receiveoutput() async def testdisconnectwithbody(self): application = getasgiapplication() scope = self.asyncrequestfactory.basescope(path="/") communicator = ApplicationCommunicator(application, scope) await communicator.sendinput({ "type": "http.request", "body": b"some body", }) await communicator.sendinput({"type": "http.disconnect"}) with self.assertRaises(asyncio.TimeoutError): await communicator.receiveoutput() async def testwrongconnectiontype(self): application = getasgiapplication() scope = self.asyncrequestfactory.basescope(path="/", type="other") To handle this correctly it looks like we'd need something like Channel's  to keep receiving from the input queue whilst dispatching the request.


Question: body_file.close()             await self.send_response(error_response, send)             return        # Get the response, using the async mode of BaseHandler.        # Try to catch a disconnect while getting response.        tasks = [            asyncio.create_task(self.run_get_response(request)),            asyncio.create_task(self.listen_for_disconnect(receive)),        ]        done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)        done, pending = done.pop(), pending.pop()        # Allow views to handle cancellation.        pending.cancel()        try:            await pending        except asyncio.CancelledError:            # Task re-raised the CancelledError as expected.            pass        try:            response = done.result()        except RequestAborted:            body_file.close()            return        except AssertionError:            body_file.close()            raise        # Send the response.        await self.send_response(response, send)    async def listen_for_disconnect(self, receive):        """Listen for disconnect from the client."""        message = await receive()        if message["type"] == "http.disconnect":            raise RequestAborted()        # This should never happen.        assert False, "Invalid ASGI message after request body: %s" % message["type"]    async def run_get_response(self, request):        """Get async response."""        # Use the async mode of BaseHandler.         response = await self.get_response_async(request)         response._handler_class = self.__class__         # Increase chunk size on file responses (ASGI servers handles low-level         # chunking).         if isinstance(response, FileResponse):             response.block_size = self.chunk_size        # Send the response.        await self.send_response(response, send)        return response      async def read_body(self, receive):         """Reads an HTTP body from an ASGI connection."""


Question: pass             else:                 if getattr(match.func, "should_append_slash", True):                    return HttpResponsePermanentRedirect("%s/" % request.path)                    return HttpResponsePermanentRedirect(                        request.get_full_path(force_append_slash=True)                    )         raise Http404      def _build_app_dict(self, request, label=None):

AdminSite.catchallview() drops query string in redirects Description introduced AdminSite.catchallview(). However, in the process it broke the ability to redirect with settings.APPENDSLASH = True when there are query strings. Provided URL: http://127.0.0.1:8000/admin/auth/foo?id=123 Expected redirect: http://127.0.0.1:8000/admin/auth/foo/?id=123 Actual redirect: http://127.0.0.1:8000/admin/auth/foo/ This seems to be because the redirect in question does not include the query strings (such as via request.META['QUERYSTRING']): return HttpResponsePermanentRedirect("%s/" % )


Question: import os import re import uuid<ins>import warnings</ins> from decimal import Decimal, DecimalException from io import BytesIO from urllib.parse import urlsplit, urlunsplit

Make URLField assume "https". Description In the assumption is made that the http (no S) is a good default scheme for URLs that do not specify a scheme when submitted. Entering in a URLField will give as cleaned data. Ref:  I think URLField should assume the safe option https. I've notified the security team, and they didn't see this as a security issue.


Question: ) from django.utils import formats from django.utils.dateparse import parse_datetime, parse_duration<ins>from django.utils.deprecation import RemovedInDjango60Warning</ins> from django.utils.duration import duration_string from django.utils.ipv6 import clean_ipv6_address from django.utils.regex_helper import _lazy_re_compile


Question: }     default_validators = [validators.URLValidator()]     def __init__(self, **kwargs):    def __init__(self, *, assume_scheme=None, **kwargs):        if assume_scheme is None:            warnings.warn(                "The default scheme will be changed from 'http' to 'https' in Django "                "6.0. Pass the forms.URLField.assume_scheme argument to silence this "                "warning.",                RemovedInDjango60Warning,                stacklevel=2,            )            assume_scheme = "http"        # RemovedInDjango60Warning: When the deprecation ends, replace with:        # self.assume_scheme = assume_scheme or "https"        self.assume_scheme = assume_scheme         super().__init__(strip=True, **kwargs)      def to_python(self, value):


Question: if value:             url_fields = split_url(value)             if not url_fields[0]:                # If no URL scheme given, assume http://                url_fields[0] = "http"                # If no URL scheme given, add a scheme.                url_fields[0] = self.assume_scheme             if not url_fields[1]:                 # Assume that if no domain is provided, that the path segment                 # contains the domain.


Question: from django.core import checks, exceptions, validators from django.db import connection, connections, router from django.db.models.constants import LOOKUP_SEP<ins>from django.db.models.enums import ChoicesMeta</ins> from django.db.models.query_utils import DeferredAttribute, RegisterLookupMixin from django.utils import timezone from django.utils.datastructures import DictWrapper

Added support for direct usage of Choices classes on model fields Description (last modified by T. Franzel) Hi, I would like to propose a feature addition on how Choices are handled when used on model fields. Currently, Field.choices only accepts iterables. This has 2 shortcommings imho: Rejecting a Choices class as argument to Field(choices=...) seems counter-intuitive. Providing the class directly currently results in a fields.E005 error. To make this more pythonic, the field should also accept the Choice class directly and deal with the variation internally. I really can't come up with a scenario where a user would want a different behavior or rather that a user would be surprised by the implicit resolution. forcing the user to expand the Choices class manually, essentially all meta information is lost. Downstream packages may benefit from this lost information. Specifically, as maintainer of drf-spectcular (OpenAPI generator for DRF), I am interested in the name of the choice set (e.g. Suit, Vehicle, Gender) and potentially also the docstring. This would greatly improve OpenAPI generation of choice sets and take out the unnecessary guesswork to find a proper name. (And if anyone wonders, the model field name is not a good candidate for a choice set name.) This PR allows to use Choices classes directly as argument, while being transparent. No behavioral changes otherwise. I marked this as dev, but it would be awesome if it could still slip into 4.2. Not sure if the feature window is still open, but this is more or less a trivial and backwards-compatible change with little risk. PR is still missing some docs, which I will write if this is considered. class Suit(models.IntegerChoices): """ All possible card categories in a deck """ DIAMOND = 1, ("Diamond") SPADE = 2, ("Spade") HEART = 3, ("Heart") CLUB = 4, ("Club") class Choiceful(models.Model): # CURRENTLY: fromenumold = models.IntegerField(choices=Suit.choices) # NEW: raised an fields.E005 prior to proposed PR. Now, retains reference # to class and transparently resolves via implicit call from_new = models.IntegerField(choices=Suit)


Question: self.unique_for_date = unique_for_date         self.unique_for_month = unique_for_month         self.unique_for_year = unique_for_year        if isinstance(choices, ChoicesMeta):            choices = choices.choices         if isinstance(choices, collections.abc.Iterator):             choices = list(choices)         self.choices = choicesdiff --git a/django/forms/fields.py b/django/forms/fields.py-- a/django/forms/fields.py++ b/django/forms/fields.py


Question: from django.core import validators from django.core.exceptions import ValidationError<ins>from django.db.models.enums import ChoicesMeta</ins> from django.forms.boundfield import BoundField from django.forms.utils import from_current_timezone, to_current_timezone from django.forms.widgets import (


Question: def __init__(self, *, choices=(), **kwargs):         super().__init__(**kwargs)<ins>        if isinstance(choices, ChoicesMeta):            choices = choices.choices</ins>         self.choices = choices      def __deepcopy__(self, memo):


Question: # Verify the session             if hasattr(user, "get_session_auth_hash"):                 session_hash = request.session.get(HASH_SESSION_KEY)                session_hash_verified = session_hash and constant_time_compare(                    session_hash, user.get_session_auth_hash()                )                if not session_hash:                    session_hash_verified = False                else:                    session_auth_hash = user.get_session_auth_hash()                    session_hash_verified = constant_time_compare(                        session_hash, session_auth_hash                    )                 if not session_hash_verified:                    request.session.flush()                    user = None                    # If the current secret does not verify the session, try                    # with the fallback secrets and stop when a matching one is                    # found.                    if session_hash and any(                        constant_time_compare(session_hash, fallback_auth_hash)                        for fallback_auth_hash in user.get_session_auth_fallback_hash()                    ):                        request.session.cycle_key()                        request.session[HASH_SESSION_KEY] = session_auth_hash                    else:                        request.session.flush()                        user = None      return user or AnonymousUser() diff --git a/django/contrib/auth/base_user.py b/django/contrib/auth/base_user.py-- a/django/contrib/auth/base_user.py++ b/django/contrib/auth/base_user.py

SECRETKEYFALLBACKS is not used for sessions Description I recently rotated my secret key, made the old one available in SECRETKEYFALLBACKS and I'm pretty sure everyone on our site is logged out now. I think the docs for SECRETKEYFALLBACKS may be incorrect when stating the following: In order to rotate your secret keys, set a new SECRETKEY and move the previous value to the beginning of SECRETKEYFALLBACKS. Then remove the old values from the end of the SECRETKEYFALLBACKS when you are ready to expire the sessions, password reset tokens, and so on, that make use of them. When looking at the Django source code, I see that the saltedhmac function uses the SECRETKEY by default and the AbstractBaseUser.getsessionauthhash method does not call salted_hmac with a value for the secret keyword argument.


Question: import unicodedata import warnings <ins>from django.conf import settings</ins> from django.contrib.auth import password_validation from django.contrib.auth.hashers import (     check_password,


Question: """         Return an HMAC of the password field.         """        return self._get_session_auth_hash()    def get_session_auth_fallback_hash(self):        for fallback_secret in settings.SECRET_KEY_FALLBACKS:            yield self._get_session_auth_hash(secret=fallback_secret)    def _get_session_auth_hash(self, secret=None):         key_salt = "django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash"         return salted_hmac(             key_salt,             self.password,            secret=secret,             algorithm="sha256",         ).hexdigest()


Question: def generate_added_indexes(self):         for (app_label, model_name), alt_indexes in self.altered_indexes.items():<ins>            dependencies = self._get_dependencies_for_model(app_label, model_name)</ins>             for index in alt_indexes["added_indexes"]:                 self.add_operation(                     app_label,

Migrations tries to add constraint before adding a foreign key. Description (last modified by Raphael Beekmann) Hello, I have a model, already created through previous migrations, and in a new migration I a new field with an UniqueConstraint. The migrations script try to create the constraint first and then the new field, resulting an error : NewModel has no field named 'category' To reproduce the bug : Create a project with two models linked together with a One-to-Many relation and an unique constraint : class Type(models.Model): name = models.CharField(maxlength=10) class Model(models.Model): name = models.CharField(maxlength=10) type = models.ForeignKey(Type, ondelete=models.SETNULL, null=True) date = models.DateField(autonow=True) class Meta: constraints = ( models.UniqueConstraint(fields=('date', 'type'), name='uniquetypefordate'), ) Create a migration file with makemigrations Add a new model with another One-to-Many relation and unique constraint. The models looks like this : class Type(models.Model): name = models.CharField(maxlength=10) class Category(models.Model): name = models.CharField(maxlength=10) class Model(models.Model): name = models.CharField(maxlength=10) type = models.ForeignKey(Type, ondelete=models.SETNULL, null=True) category = models.ForeignKey(Category, ondelete=models.SETNULL, null=True) date = models.DateField(autonow=True) class Meta: constraints = ( models.UniqueConstraint(fields=('date', 'type'), name='uniquetypefordate'), models.UniqueConstraint(fields=('date', 'category'), name='uniquecategoryfordate'), ) Create a new migration file. The order of the migration's steps are incorrect and the migration crash : class Migration(migrations.Migration): dependencies = [ ('app', '0001initial'), ] operations = [ migrations.CreateModel( name='Category', fields=[ ('id', models.BigAutoField(autocreated=True, primarykey=True, serialize=False, verbosename='ID')), ('name', models.CharField(maxlength=10)), ], ), migrations.AddConstraint( modelname='model', constraint=models.UniqueConstraint(fields=('date', 'category'), name='uniquecategoryfordate'), ), migrations.AddField( modelname='model', name='category', field=models.ForeignKey(null=True, , to='app.category'), ), ]


Question: model_name=model_name,                         index=index,                     ),<ins>                    dependencies=dependencies,</ins>                 )      def generate_removed_indexes(self):


Question: app_label,             model_name,         ), alt_constraints in self.altered_constraints.items():<ins>            dependencies = self._get_dependencies_for_model(app_label, model_name)</ins>             for constraint in alt_constraints["added_constraints"]:                 self.add_operation(                     app_label,


Question: model_name=model_name,                         constraint=constraint,                     ),<ins>                    dependencies=dependencies,</ins>                 )      def generate_removed_constraints(self):


Question: dependencies.append((through_app_label, through_object_name, None, True))         return dependencies <ins>    def _get_dependencies_for_model(self, app_label, model_name):        """Return foreign key dependencies of the given model."""        dependencies = []        model_state = self.to_state.models[app_label, model_name]        for field in model_state.fields.values():            if field.is_relation:                dependencies.extend(                    self._get_dependencies_for_foreign_key(                        app_label,                        model_name,                        field,                        self.to_state,                    )                )        return dependencies</ins>     def _get_altered_foo_together_operations(self, option_name):         for app_label, model_name in sorted(self.kept_model_keys):             old_model_name = self.renamed_models.get(


Question: # Encoding isn't set to prevent browsers from automatically                 # uncompressing files.                 content_type = {                    "br": "application/x-brotli",                     "bzip2": "application/x-bzip",                    "compress": "application/x-compress",                     "gzip": "application/gzip",                     "xz": "application/x-xz",                 }.get(encoding, content_type)

Improper guessing of Mime Type for "br" and "Z" file types Description Below FileResponse will set the content type as text/html, even if the last file extension is "Z' or "br". FileResponse(open('', 'rb')) FileResponse(open('', 'rb'))


Question: if select:             self.append_annotation_mask([alias])         else:            self.set_annotation_mask(set(self.annotation_select).difference({alias}))            annotation_mask = (                value                for value in dict.fromkeys(self.annotation_select)                if value != alias            )            self.set_annotation_mask(annotation_mask)         self.annotations[alias] = annotation      def resolve_expression(self, query, *args, **kwargs):

Querysets: annotate() columns are forced into a certain position which may disrupt union() Description (Reporting possible issue found by a user on #django) Using values() to force selection of certain columns in a certain order proved useful unioning querysets with union() for the aforementioned user. The positioning of columns with annotate() is not controllable with values() and has the potential to disrupt union() unless this fact is known and the ordering done in a certain way to accommodate it. I'm reporting this mainly for posterity but also as a highlight that perhaps this should be mentioned in the documentation. I'm sure there are reasons why the annotations are appended to the select but if someone feels that this is changeable then it would be a bonus outcome.


Question: if names is None:             self.annotation_select_mask = None         else:            self.annotation_select_mask = set(names)            self.annotation_select_mask = list(dict.fromkeys(names))         self._annotation_select_cache = None      def append_annotation_mask(self, names):         if self.annotation_select_mask is not None:            self.set_annotation_mask(self.annotation_select_mask.union(names))            self.set_annotation_mask((*self.annotation_select_mask, *names))      def set_extra_mask(self, names):         """


Question: return {}         elif self.annotation_select_mask is not None:             self._annotation_select_cache = {                k: v                for k, v in self.annotations.items()                if k in self.annotation_select_mask                k: self.annotations[k]                for k in self.annotation_select_mask                if k in self.annotations             }             return self._annotation_select_cache         else:


Question: "update.tests.AdvancedTests."                 "test_update_ordered_by_inline_m2m_annotation",                 "update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation",                "update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation_desc",             },         }         if self.connection.mysql_is_mariadb and (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

() on querysets in descending order by annotations. Description When I execute Model.objects.annotate(messagelength=Length('message')).orderby('-messagelength').update(text="Can I come on board?") I get the error FieldError: Cannot resolve keyword 'messagelength' into field. Choices are: message, id, text, x because the code ignores the descending case.


Question: # Inline annotations in order_by(), if possible.         new_order_by = []         for col in query.order_by:            if annotation := query.annotations.get(col):            alias = col            descending = False            if isinstance(alias, str) and alias.startswith("-"):                alias = alias.removeprefix("-")                descending = True            if annotation := query.annotations.get(alias):                 if getattr(annotation, "contains_aggregate", False):                     raise exceptions.FieldError(                         f"Cannot update when ordering by an aggregate: {annotation}"                     )                if descending:                    annotation = annotation.desc()                 new_order_by.append(annotation)             else:                 new_order_by.append(col)


Question: # Lookups on nonexistent fields are ok, since they're ignored                 # later.                 break            # It is allowed to filter on values that would be found from local            # model anyways. For example, if you filter on employee__department__id,            # then the id value would be found already from employee__department_id.             if not prev_field or (                 prev_field.is_relation                and field not in prev_field.path_infos[-1].target_fields                and field not in model._meta.parents.values()                and field is not model._meta.auto_field                and (                    model._meta.auto_field is None                    or part not in getattr(prev_field, "to_fields", [])                )             ):                 relation_parts.append(part)             if not getattr(field, "path_infos", None):

ModelAdmin.lookupallowed() incorrectly raises DisallowedModelAdminLookup lookup with foreign key as primary key Description (last modified by Tim Graham) Wrote a failing test for to demonstrate - same test/code passes on 1.8 def testlookupallowedforeignprimary(self): class Country(models.Model): name = models.CharField(maxlength=256) class Place(models.Model): country = models.ForeignKey(Country, models.CASCADE) class Restaurant(models.Model): place = models.OneToOneField(Place, models.CASCADE, primarykey=True) class Waiter(models.Model): restaurant = models.ForeignKey(Restaurant, models.CASCADE) class WaiterAdmin(ModelAdmin): listfilter = [ 'restaurantplacecountry', ] ma = WaiterAdmin(Waiter, ) self.assertIs(ma.lookupallowed('restaurantplacecountry', 'testvalue'), True) I think this is caused by the admin thinking that having a foreign key field as a primary key is the same as concrete inheritance. So when you try and check lookups for restaurantplacecountry it thinks 'place' is the concrete parent of 'restaurant' and shortcuts it to restaurantcountry which isn't in 'listfilter'. And you can't add restaurantcountry to list_filter because country isn't actually on restaurant.


Question: # Sort imports by the package / module to be imported (the part after         # "from" in "from ... import ..." or after "import" in "import ...").        sorted_imports = sorted(imports, key=lambda i: i.split()[1])        # First group the "import" statements, then "from ... import ...".        sorted_imports = sorted(            imports, key=lambda i: (i.split()[0] == "from", i.split()[1])        )         items["imports"] = "\n".join(sorted_imports) + "\n" if imports else ""         if migration_imports:             items["imports"] += (

Migration import ordering violates coding style and isort defaults Description New migration files are generated with imports sorted by module, independent of import style. For example: import datetime from import migrations, models import time The Django coding style specifies: Place all import module statements before from module import objects in each section. This guidance is the same as what isort does by default, as documented here. Newly generated migrations can fail isort for this reason. This would mean migration files should instead be generated like this: import datetime import time from import migrations, models For reference, previous issues related to migration import sorting: , .


Question: # Return pseudo-ISO dates with zeros for any unselected values,                 # e.g. '2017-0-23'.                 return "%s-%s-%s" % (y or 0, m or 0, d or 0)<ins>            except OverflowError:                return "0-0-0"</ins>             return date_value.strftime(input_format)         return data.get(name)

SelectDateWidget can crash with OverflowError. Description Given a relatively common view like this: from django import forms from import SelectDateWidget from import HttpResponse class ReproForm(forms.Form): mydate = forms.DateField(widget=SelectDateWidget()) def reproview(request): form = ReproForm(request.GET) # for ease of reproducibility if form.isvalid(): return HttpResponse("ok") else: return HttpResponse("not ok") urlpatterns = [path('repro/', views.reproview, name='repro')] A user can trigger a server crash, reproducible by running locally and visiting http://127.0.0.1:8000/repro/?mydateday=1&mydatemonth=1&mydateyear=, which results in [...] - ERROR - django.request: Internal Server Error: /repro/ Traceback (most recent call last): [...] File "[...]/site-", line 1160, in valuefromdatadict datevalue = (int(y), int(m), int(d)) OverflowError: signed integer is greater than maximum This can be triggered similarly for a post request. The issue happens as part of the validation logic run in form.isvalid, specifically, when calling the SelectDateWidget.valuefromdatadict, where the user-controlled value is converted into a date without guarding against a possible OverflowError. Specifically, y, m and d are user controlled, and the code does this: date_value = (int(y), int(m), int(d)) When large integers (larger than sys.maxsize) are supplied to date's constructor it will throw an OverflowError: import datetime, sys (sys.maxsize+1, 3, 4) Traceback (most recent call last): File "<stdin", line 1, in <moduleOverflowError: Python int too large to convert to C long


Question: async def get_response_async(self, request):         response = await super().get_response_async(request)         response._resource_closers.append(request.close)<ins>        # FileResponse is not async compatible.        if response.streaming and not response.is_async:            _iterator = response.streaming_content            async def awrapper():                for part in await sync_to_async(list)(_iterator):                    yield part            response.streaming_content = awrapper()</ins>         return response

ASGIStaticFilesHandler raises warning. Description (last modified by Carlton Gibson) async support to StreamingHttpResponse for Django 4.2. With Django 4.2rc1 and Daphne, ASGIStaticFilesHandler raises a warning about the incorrect iterator type being used: Warning: StreamingHttpResponse must consume synchronous iterators in order to serve them asynchronously. Use an asynchronous iterator instead. This is because FileResponse is not async compatible (nor is that likely, since neither is open()). A new project with: INSTALLEDAPPS = [ "daphne", ... ] ASGIAPPLICATION = "" runserver, and then visit any page serving static files such as /admin/. The fix is to have ASGIStaticFilesHandler adapt the iterator in the same way StreamingHttpResponse does.


Question: # For lookups spanning over relationships, show the error                 # from the model on which the lookup failed.                 raise<del>            elif name in self.annotations:                raise FieldError(                    "Cannot select the '%s' alias. Use annotate() to promote "                    "it." % name                )</del>             else:                 names = sorted(                     [

values() doesn't mention annotations as valid choices on resolving error Description When doing Book.objects.annotate(annotation=Value(1)).values("annotationtype") The resulting FieldError doesn't mention annotation as a valid choice FieldError: Cannot resolve keyword 'annotationtypo' into field. Choices are: age, book, bookcontactset, friends, id, name


Question: extra_names.append(f)                     elif f in self.annotation_select:                         annotation_names.append(f)                    elif f in self.annotations:                        raise FieldError(                            f"Cannot select the '{f}' alias. Use annotate() to "                            "promote it."                        )                     else:                        # Call `names_to_path` to ensure a FieldError including                        # annotations about to be masked as valid choices if                        # `f` is not resolvable.                        if self.annotation_select:                            self.names_to_path(f.split(LOOKUP_SEP), self.model._meta)                         field_names.append(f)             self.set_extra_mask(extra_names)             self.set_annotation_mask(annotation_names)


Question: processors = template.engine.template_context_processors + self._processors         updates = {}         for processor in processors:            updates.update(processor(self.request))            context = processor(self.request)            try:                updates.update(context)            except TypeError as e:                raise TypeError(                    f"Context processor {processor.__qualname__} didn't return a "                    "dictionary."                ) from e         self.dicts[self._processors_index] = updates          try:

Improve error message for incorrect context processors Description If you write a template context processor but return the wrong type, for example None by missing the return statement: def data(request): data = {"something": True} Then currently the error message is very mysterious: Internal Server Error: / Traceback (most recent call last): ... File "/..../site-", line 61, in render return (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/..../site-", line 173, in render with context.bindtemplate(self): File "", line 137, in enter return next() ^^^^^^^^^^^^^^ File "/..../site-", line 254, in bindtemplate (processor(self.request)) TypeError: 'NoneType' object is not iterable If a view returns the wrong type, Django raises a nice error message: The view didn't return an HttpResponse object. It returned None instead. I suggest we do the same for context processors. If we use try/except around the () line, it will not slow down normal processing any noticeable amount, thanks to Python 3.11's “zero-cost” exception handling:


Question: raise ValueError(                     "Cannot call lazy() with both bytes and text return types."                 )            if cls._delegate_text:                cls.__str__ = cls.__text_cast            elif cls._delegate_bytes:            if cls._delegate_bytes:                 cls.__bytes__ = cls.__bytes_cast          @classmethod

DateField.inputformats cannot be printed Description I am trying to display the input formats for This seems to fail at the moment when using str, while repr works. Example code: from import DateField print(repr(DateField().inputformats)) print(DateField().input_formats) This will result in the following error: Traceback (most recent call last): File "<console", line 1, in <moduleTypeError: str returned non-string (type list) I would have expected the string representation to be available as well instead of failing with an internal Python error.


Question: return __wrapper__         def __text_cast(self):            return func(*self.__args, **self.__kw)         def __bytes_cast(self):             return bytes(func(*self.__args, **self.__kw))         def __bytes_cast_encoded(self):            return func(*self.__args, **self.__kw).encode()         def __cast(self):             if self._delegate_bytes:                 return self.__bytes_cast()            elif self._delegate_text:                return self.__text_cast()             else:                 return func(*self.__args, **self.__kw)


Question: from django.utils.functional import cached_property from django.utils.http import RFC3986_SUBDELIMS, escape_leading_slashes from django.utils.regex_helper import _lazy_re_compile, normalizefrom django.utils.translation import get_languagefrom django.utils.translation import get_language, get_supported_language_variant  from .converters import get_converter from .exceptions import NoReverseMatch, Resolver404

i18npatterns() not respecting prefixdefaultlanguage=False Description (last modified by Oussama Jarrousse) In my django project file I have the following setup: from import i18npatterns from django.contrib import admin from import include from import path urlpatterns = [] urlpatterns += i18npatterns( path("admin/", ), prefixdefaultlanguage=False ) In versions Django==4.1.7 (or prior), I was able to navigating to /admin/ without having to add the language prefix. Django==4.2.0, navigating to /admin/ will cause a HTTP 302 and only /en/admin/ works... although prefixdefault_language=False is explicitly defined. This change broke my API upon backend packages upgrade from 4.1.7 to 4.2.0


Question: @property     def language_prefix(self):         language_code = get_language() or settings.LANGUAGE_CODE        if language_code == settings.LANGUAGE_CODE and not self.prefix_default_language:        default_language = get_supported_language_variant(settings.LANGUAGE_CODE)        if language_code == default_language and not self.prefix_default_language:             return ""         else:             return "%s/" % language_codediff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py-- a/django/utils/translation/__init__.py++ b/django/utils/translation/__init__.py


Question: "get_language_from_request",     "get_language_info",     "get_language_bidi",<ins>    "get_supported_language_variant",</ins>     "check_for_language",     "to_language",     "to_locale",


Question: message = _("Ensure this value is a multiple of step size %(limit_value)s.")     code = "step_size"     def __init__(self, limit_value, message=None, offset=None):        super().__init__(limit_value, message)        if offset is not None:            self.message = _(                "Ensure this value is a multiple of step size %(limit_value)s, "                "starting from %(offset)s, e.g. %(offset)s, %(valid_value1)s, "                "%(valid_value2)s, and so on."            )        self.offset = offset    def __call__(self, value):        if self.offset is None:            super().__call__(value)        else:            cleaned = self.clean(value)            limit_value = (                self.limit_value() if callable(self.limit_value) else self.limit_value            )            if self.compare(cleaned, limit_value):                offset = cleaned.__class__(self.offset)                params = {                    "limit_value": limit_value,                    "offset": offset,                    "valid_value1": offset + limit_value,                    "valid_value2": offset + 2 * limit_value,                }                raise ValidationError(self.message, code=self.code, params=params)     def compare(self, a, b):        return not math.isclose(math.remainder(a, b), 0, abs_tol=1e-9)        offset = 0 if self.offset is None else self.offset        return not math.isclose(math.remainder(a - offset, b), 0, abs_tol=1e-9)   @deconstructiblediff --git a/django/forms/fields.py b/django/forms/fields.py-- a/django/forms/fields.py++ b/django/forms/fields.py

StepValueValidator does not take into account minvalue Description If you define a number input with <input type="number" min=1 step=2, client side this will only allow positive odd numbers. We could generate the same input in a Django form with IntegerField(minvalue=1, stepsize=2) and Field.localize is False, which would then use MinValueValidator and StepValueValidator. We then get into a problem as StepValueValidator always uses 0 as the base, so stepsize=2 only even numbers are allowed. This then conflicts with the client side validation, and the user cannot submit any value for the input. I'm unsure if this is a bug or whether this is just a configuration problem, apologies if so, but the behaviour does seem to me to conflict with how min and step is handled by browsers.


Question: if min_value is not None:             self.validators.append(validators.MinValueValidator(min_value))         if step_size is not None:            self.validators.append(validators.StepValueValidator(step_size))            self.validators.append(                validators.StepValueValidator(step_size, offset=min_value)            )      def to_python(self, value):         """


Question: # Translators: String used to replace omitted page numbers in elided page     # range generated by paginators, e.g. [1, 2, '…', 5, 6, 7, '…', 9, 10].     ELLIPSIS = _("…")    def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True):    default_error_messages = {        "invalid_page": _("That page number is not an integer"),        "min_page": _("That page number is less than 1"),        "no_results": _("That page contains no results"),    }    def __init__(        self,        object_list,        per_page,        orphans=0,        allow_empty_first_page=True,        error_messages=None,    ):         self.object_list = object_list         self._check_object_list_is_ordered()         self.per_page = int(per_page)         self.orphans = int(orphans)         self.allow_empty_first_page = allow_empty_first_page        self.error_messages = (            self.default_error_messages            if error_messages is None            else self.default_error_messages | error_messages        )      def __iter__(self):         for page_number in self.page_range:

Error messages customization and internationalization in Paginator class Description There is no ability to change error messages raised by Paginator.validate_number method.


Question: raise ValueError             number = int(number)         except (TypeError, ValueError):            raise PageNotAnInteger(_("That page number is not an integer"))            raise PageNotAnInteger(self.error_messages["invalid_page"])         if number < 1:            raise EmptyPage(_("That page number is less than 1"))            raise EmptyPage(self.error_messages["min_page"])         if number > self.num_pages:            raise EmptyPage(_("That page contains no results"))            raise EmptyPage(self.error_messages["no_results"])         return number      def get_page(self, number):


Question: logger = logging.getLogger("django.request")  <ins>def get_script_prefix(scope):    """    Return the script prefix to use from either the scope or a setting.    """    if settings.FORCE_SCRIPT_NAME:        return settings.FORCE_SCRIPT_NAME    return scope.get("root_path", "") or ""</ins> class ASGIRequest(HttpRequest):     """     Custom request subclass that decodes from an ASGI-standard request dict

ASGIRequest doesn't respect settings.FORCESCRIPTNAME. Description For example, I have settings.FORCESCRIPTNAME = '/some-prefix' I start a django server with command: daphne And I navigate to the http://localhost:8000/admin/login, and see the login form action url is "/admin/login" which is wrong, which should be "/some-prefix/admin/login"


Question: self._post_parse_error = False         self._read_started = False         self.resolver_match = None        self.script_name = self.scope.get("root_path", "")        self.script_name = get_script_prefix(scope)         if self.script_name:             # TODO: Better is-prefix checking, slash handling?             self.path_info = scope["path"].removeprefix(self.script_name)


Question: except RequestAborted:             return         # Request is complete and can be served.        set_script_prefix(self.get_script_prefix(scope))        set_script_prefix(get_script_prefix(scope))         await signals.request_started.asend(sender=self.__class__, scope=scope)         # Get the request and check for basic issues.         request, error_response = self.create_request(scope, body_file)


Question: (position + cls.chunk_size) >= len(data),             )             position += cls.chunk_size<del>    def get_script_prefix(self, scope):        """        Return the script prefix to use from either the scope or a setting.        """        if settings.FORCE_SCRIPT_NAME:            return settings.FORCE_SCRIPT_NAME        return scope.get("root_path", "") or ""</del>


Question: class Chr(Transform):     function = "CHR"     lookup_name = "chr"<ins>    output_field = CharField()</ins>      def as_mysql(self, compiler, connection, **extra_context):         return super().as_sql(

Annotating with Chr raises ValueError Description (last modified by Jacob Walls) Author.objects.annotate(firstletter=Chr(Value(77))) File "", line 369, in <lambdaelse int(value) ValueError: invalid literal for int() with base 10: 'M' I'm suggesting in Chr we can set outputfield = CharField() to prevent this.


Question: id="admin.E108",                     )                 ]        if isinstance(field, models.ManyToManyField):        if isinstance(field, models.ManyToManyField) or (            getattr(field, "rel", None) and field.rel.field.many_to_one        ):             return [                 checks.Error(                    "The value of '%s' must not be a ManyToManyField." % label,                    f"The value of '{label}' must not be a many-to-many field or a "                    f"reverse foreign key.",                     obj=obj.__class__,                     id="admin.E109",                 )

Admin check for reversed foreign key used in "listdisplay" Description Currently the admin site checks and reports an admin.E109 system check error when a ManyToManyField is declared within the listdisplay values. But, when a reversed foreign key is used there is no system error reported: System check identified no issues (0 silenced). April 10, 2023 - 19:04:29 Django version 5.0.dev20230410064954, using settings 'projectfromrepo.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C. and visiting the admin site for the relevant model results in the following error: TypeError: createreversemanytoonemanager.() missing 1 required keyword-only argument: 'manager' [10/Apr/2023 19:30:40] "GET /admin/testapp/question/ HTTP/1.1" 500 Ideally, using a reversed foreign key would also result in a system check error instead of a 500 response. To reproduce, follow the Question and Choice models from the Django Tutorial and add the following to the QuestionAdmin: listdisplay = ["questiontext", "choiceset"] Then, visit /admin/testapp/question/ and the following traceback is returned: Internal Server Error: /admin/testapp/question/ Traceback (most recent call last): File "", line 681, in getfield return self.fieldsmap[fieldname] ~~~^^^^^^^^^^^^ KeyError: 'choiceset' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 288, in lookupfield f = getnongfkfield(opts, name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 319, in getnongfkfield field = opts.getfield(name) ^^^^^^^^^^^^^^^^^^^^ File "", line 683, in getfield raise FieldDoesNotExist( Question has no field named 'choiceset' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 55, in inner response = getresponse(request) ^^^^^^^^^^^^^^^^^^^^^ File "", line 220, in getresponse response = () ^^^^^^^^^^^^^^^^^ File "", line 111, in render self.content = self.renderedcontent ^^^^^^^^^^^^^^^^^^^^^ File "", line 89, in renderedcontent return (context,


Question: id="admin.E108",                     )                 ]        if isinstance(field, models.ManyToManyField):        if isinstance(field, models.ManyToManyField) or (            getattr(field, "rel", None) and field.rel.field.many_to_one        ):             return [                 checks.Error(                    "The value of '%s' must not be a ManyToManyField." % label,                    f"The value of '{label}' must not be a many-to-many field or a "                    f"reverse foreign key.",                     obj=obj.__class__,                     id="admin.E109",                 )

self.request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 61, in render return (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 171, in render return (context) ^^^^^^^^^^^^^^^^^^^^^ File "", line 163, in render return (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in render return SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in <listcompreturn SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 962, in renderannotated return (context) ^^^^^^^^^^^^^^^^^^^^ File "", line 157, in render return (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 163, in render return (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in render return SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in <listcompreturn SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 962, in renderannotated return (context) ^^^^^^^^^^^^^^^^^^^^ File "", line 157, in render return (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 163, in render return (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in render return SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in <listcompreturn SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 962, in renderannotated return (context) ^^^^^^^^^^^^^^^^^^^^ File "", line 63, in render result = (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in render return SafeString("".join([node.


Question: id="admin.E108",                     )                 ]        if isinstance(field, models.ManyToManyField):        if isinstance(field, models.ManyToManyField) or (            getattr(field, "rel", None) and field.rel.field.many_to_one        ):             return [                 checks.Error(                    "The value of '%s' must not be a ManyToManyField." % label,                    f"The value of '{label}' must not be a many-to-many field or a "                    f"reverse foreign key.",                     obj=obj.__class__,                     id="admin.E109",                 )

renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in <listcompreturn SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 962, in renderannotated return (context) ^^^^^^^^^^^^^^^^^^^^ File "", line 63, in render result = (context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in render return SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1001, in <listcompreturn SafeString("".join([node.renderannotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 962, in renderannotated return (context) ^^^^^^^^^^^^^^^^^^^^ File "", line 45, in render return super().render(context) ^^^^^^^^^^^^^^^^^^^^^^^ File "", line 258, in render dict = (resolvedargs, resolvedkwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 340, in resultlist "results": list(results(cl)), ^^^^^^^^^^^^^^^^^ File "", line 316, in results yield ResultList(None, itemsforresult(cl, res, None)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 307, in init super().init(items) File "", line 217, in itemsforresult f, attr, value = lookupfield(fieldname, result, cl.modeladmin) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 301, in lookupfield value = attr() ^^^^^^ TypeError: createreversemanytoonemanager.() missing 1 required keyword-only argument: 'manager' [10/Apr/2023 19:30:40] "GET /admin/testapp/question/ HTTP/1.1" 500


Question: else self.get_list_display(request)         )     def lookup_allowed(self, lookup, value):    # RemovedInDjango60Warning: when the deprecation ends, replace with:    # def lookup_allowed(self, lookup, value, request):    def lookup_allowed(self, lookup, value, request=None):         from django.contrib.admin.filters import SimpleListFilter          model = self.model

lookupallowed fails to consider dynamic listfilter Description Currently, lookupallowed iterates over self.listfilter to determine valid params. This is technically incorrect since the introduction of getlistfilter() on ModelAdmin in 1.5, because it is possible to define a ModelAdmin such that self.listfilter is () but getlistfilter yields SimpleListFilter classes. To correct it, the above code would need to change from: for filteritem in self.listfilter: to for filteritem in self.getlistfilter(request): The problem is that now lookupallowed needs to accept request so that it can pass it back to getlistfilter In Django itself, that's actually reasonably acceptable as a change, because it's used infrequently - the only place it's actually used is in ChangeList.getfilters, which has access to the request. However, it is overridden in the wild without accept args, *kwargs, so it'd not be easy to provide a clean upgrade path.


Question: # Either a local field filter, or no fields at all.             return True         valid_lookups = {self.date_hierarchy}        for filter_item in self.list_filter:        # RemovedInDjango60Warning: when the deprecation ends, replace with:        # for filter_item in self.get_list_filter(request):        list_filter = (            self.get_list_filter(request) if request is not None else self.list_filter        )        for filter_item in list_filter:             if isinstance(filter_item, type) and issubclass(                 filter_item, SimpleListFilter             ):diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py-- a/django/contrib/admin/views/main.py++ b/django/contrib/admin/views/main.py


Question: <ins>import warnings</ins> from datetime import datetime, timedelta  from django import forms


Question: from django.db.models import Exists, F, Field, ManyToOneRel, OrderBy, OuterRef from django.db.models.expressions import Combinable from django.urls import reversefrom django.utils.deprecation import RemovedInDjango60Warning from django.utils.http import urlencodefrom django.utils.inspect import func_supports_parameter from django.utils.timezone import make_aware from django.utils.translation import gettext


Question: may_have_duplicates = False         has_active_filters = False         supports_request = func_supports_parameter(            self.model_admin.lookup_allowed, "request"        )        if not supports_request:            warnings.warn(                f"`request` must be added to the signature of "                f"{self.model_admin.__class__.__qualname__}.lookup_allowed().",                RemovedInDjango60Warning,            )         for key, value_list in lookup_params.items():             for value in value_list:                if not self.model_admin.lookup_allowed(key, value):                params = (key, value, request) if supports_request else (key, value)                if not self.model_admin.lookup_allowed(*params):                     raise DisallowedModelAdminLookup(f"Filtering by {key} not allowed")          filter_specs = []diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py-- a/django/contrib/auth/admin.py++ b/django/contrib/auth/admin.py


Question: ),         ] + super().get_urls()     def lookup_allowed(self, lookup, value):    # RemovedInDjango60Warning: when the deprecation ends, replace with:    # def lookup_allowed(self, lookup, value, request):    def lookup_allowed(self, lookup, value, request=None):         # Don't allow lookups involving passwords.         return not lookup.startswith("password") and super().lookup_allowed(            lookup, value            lookup, value, request         )      @sensitive_post_parameters_m


Question: # after their corresponding image field don't stay cleared by         # Model.__init__, see bug #11196.         # Only run post-initialization dimension update on non-abstract models        if not cls._meta.abstract:        # with width_field/height_field.        if not cls._meta.abstract and (self.width_field or self.height_field):             signals.post_init.connect(self.update_dimension_fields, sender=cls)      def update_dimension_fields(self, instance, force=False, *args, **kwargs):

ImageField unnecessarily adds a postinit signal handler to the model Description While debugging some performance issues in a Django app, I found a codepath where most of the time was being spent on initializing Django models after fetching from the DB. It turns out that 30% of the time was being spent on evaluating postinit signals because we were using ImageField. However, the postinit signal handler is a noop because we don't use the widthfield / heightfield. If widthfield and heightfield are not set, removing the postinit signal should have no effect since the signal handler will return right away. Removing this signal handler gave us a 30-40% speedup on initializing models where ImageField was used.


Question: Dimensions can be forced to update with force=True, which is how         ImageFileDescriptor.__set__ calls this method.         """        # Nothing to update if the field doesn't have dimension fields or if        # the field is deferred.        has_dimension_fields = self.width_field or self.height_field        if not has_dimension_fields or self.attname not in instance.__dict__:        # Nothing to update if the field is deferred.        if self.attname not in instance.__dict__:             return          # getattr will call the ImageFileDescriptor's __get__ method, which


Question: @register.filter(is_safe=True) def random(value):     """Return a random item from the list."""    return random_module.choice(value)    try:        return random_module.choice(value)    except IndexError:        return ""   @register.filter("slice", is_safe=True)

"random" template filter crashes on an empty list. Description first/last filters applied to a empty list do not raise anything and just returns a empty page. Example: {{ listvar|last }} Rendered with: def index(request): return render(request, '', context={"listvar": []}) Will result in empty page: [26/Apr/2023 09:15:49] "GET / HTTP/1.1" 200 0 As I undestand, 0 means no any characters in response content. The similar situation is with the first tag: {{ listvar|first }} While the random tag raises the IndexError and causes the server to response 500.