[Django] #34001: ForeignKey.formfield(): allow override for all kwargs

16 views
Skip to first unread message

Django

unread,
Sep 9, 2022, 6:49:36 PM9/9/22
to django-...@googlegroups.com
#34001: ForeignKey.formfield(): allow override for all kwargs
-----------------------------------------+------------------------
Reporter: James Pic | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Proposing this:


{{{
commit ec0cae2e2d0bc6ae31cf39e7c4a067810cff8fb4 (HEAD -> main,
origin/formfield)
Author: jpic <jp...@yourlabs.org>
Date: Sat Sep 10 00:47:17 2022 +0200

ForeignKey.formfield(): set defaults if necessary (allow override)

Allow to override defaults used by formfield() in ForeignKey
subclasses.

diff --git a/django/db/models/fields/related.py
b/django/db/models/fields/related.py
index 63ed2ff4c7..f9c6ff3be8 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -1164,15 +1164,21 @@ class ForeignKey(ForeignObject):
"its related model %r has not been loaded yet"
% (self.name, self.remote_field.model)
)
- return super().formfield(
- **{
- "form_class": forms.ModelChoiceField,
- "queryset":
self.remote_field.model._default_manager.using(using),
- "to_field_name": self.remote_field.field_name,
- **kwargs,
- "blank": self.blank,
- }
- )
+
+ if "form_class" not in kwargs:
+ kwargs["form_class"] = forms.ModelChoiceField
+
+ if "queryset" not in kwargs:
+ kwargs["queryset"] =
self.remote_field.model._default_manager.using(
+ using)
+
+ if "to_field_name" not in kwargs:
+ kwargs["to_field_name"] = self.remote_field.field_name
+
+ if "blank" not in kwargs:
+ kwargs["blank"] = self.blank
+
+ return super().formfield(**kwargs)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34001>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 9, 2022, 6:50:41 PM9/9/22
to django-...@googlegroups.com
#34001: ForeignKey.formfield(): allow override for all kwargs
-------------------------------------+-------------------------------------

Reporter: James Pic | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by James Pic):

* cc: James Pic (added)
* type: Uncategorized => Cleanup/optimization
* component: Uncategorized => Database layer (models, ORM)


Old description:

> Proposing this:
>

New description:

Proposing this:

Also an optimization.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34001#comment:1>

Django

unread,
Sep 9, 2022, 6:54:11 PM9/9/22
to django-...@googlegroups.com
#34001: ForeignKey.formfield(): allow override for all kwargs
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by James Pic:

Old description:

> Proposing this:
>

> Also an optimization.

New description:

Proposing to change ForeignKey.formfield() to allow overriding form_class
and all other defaults like with other fields such as fields.UUIDField, we
can optimize the code at the same time.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34001#comment:2>

Django

unread,
Sep 10, 2022, 4:09:48 AM9/10/22
to django-...@googlegroups.com
#34001: ForeignKey.formfield(): allow override for all kwargs
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => needsinfo


Comment:

Thanks for the ticket, however I'm a bit confused. Is it not possible? The
values from `kwargs` should override `form_class`, `queryset`, and
`to_field_name` in `**{..., **kwargs}`. The only difference is in `blank`
but in this case we should keep the value from a model field.

--
Ticket URL: <https://code.djangoproject.com/ticket/34001#comment:3>

Django

unread,
Sep 10, 2022, 6:58:43 AM9/10/22
to django-...@googlegroups.com
#34001: ForeignKey.formfield(): allow override for all kwargs
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by James Pic):

You are correct, my bad, should have went to sleep instead :P

--
Ticket URL: <https://code.djangoproject.com/ticket/34001#comment:4>

Django

unread,
Sep 10, 2022, 6:59:10 AM9/10/22
to django-...@googlegroups.com
#34001: ForeignKey.formfield(): allow override for all kwargs
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by James Pic):

* resolution: needsinfo => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/34001#comment:5>

Django

unread,
Sep 10, 2022, 9:25:31 AM9/10/22
to django-...@googlegroups.com
#34001: ForeignKey.formfield(): allow override for all kwargs
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* resolution: fixed => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/34001#comment:6>

Reply all
Reply to author
Forward
0 new messages