Opened 7 years ago

Closed 7 years ago

#28873 closed Cleanup/optimization (fixed)

Use dict.setdefault() to set model and form field defaults

Reported by: Дилян Палаузов Owned by: nobody
Component: Core (Other) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -1645,7 +1645,7 @@ class EmailField(CharField):
 
     def __init__(self, *args, **kwargs):
         # max_length=254 to be compliant with RFCs 3696 and 5321
-        kwargs['max_length'] = kwargs.get('max_length', 254)
+        kwargs.setdefault('max_length', 254)
         super().__init__(*args, **kwargs)
 
     def deconstruct(self):
@@ -1671,7 +1671,7 @@ class FilePathField(Field):
                  recursive=False, allow_files=True, allow_folders=False, **kwargs):
         self.path, self.match, self.recursive = path, match, recursive
         self.allow_files, self.allow_folders = allow_files, allow_folders
-        kwargs['max_length'] = kwargs.get('max_length', 100)
+        kwargs.setdefault('max_length', 100)
         super().__init__(verbose_name, name, **kwargs)
 
     def check(self, **kwargs):
@@ -2251,7 +2251,7 @@ class URLField(CharField):
     description = _("URL")
 
     def __init__(self, verbose_name=None, name=None, **kwargs):
-        kwargs['max_length'] = kwargs.get('max_length', 200)
+        kwargs.setdefault('max_length', 200)
         super().__init__(verbose_name, name, **kwargs)
 
     def deconstruct(self):
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
--- a/django/db/models/fields/files.py
+++ b/django/db/models/fields/files.py
@@ -226,7 +226,7 @@ class FileField(Field):
         self.storage = storage or default_storage
         self.upload_to = upload_to
 
-        kwargs['max_length'] = kwargs.get('max_length', 100)
+        kwargs.setdefault('max_length', 100)
         super().__init__(verbose_name, name, **kwargs)
 
     def check(self, **kwargs):
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -809,7 +809,7 @@ class ForeignKey(ForeignObject):
             on_delete=on_delete,
         )
 
-        kwargs['db_index'] = kwargs.get('db_index', True)
+        kwargs.setdefault('db_index', True)
 
         super().__init__(to, on_delete, from_fields=['self'], to_fields=[to_field], **kwargs)
 

Change History (2)

comment:1 by Tim Graham, 7 years ago

Component: UncategorizedCore (Other)
Has patch: set
Summary: More of dict.setdefaultUse dict.setdefault() to set model and form field defaults
Triage Stage: UnreviewedReady for checkin
Type: UncategorizedCleanup/optimization

PR from the patch.

comment:2 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 87c76aa:

Fixed #28873 -- Used dict.setdefault() to set model and form field defaults.

Note: See TracTickets for help on using tickets.
Back to Top