[Django] #21528: improve Django Doc with an example for formfield_for_foreignkey accessing parent ID

38 views
Skip to first unread message

Django

unread,
Nov 29, 2013, 5:33:32 AM11/29/13
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------+--------------------
Reporter: anonymous | Owner: nobody
Type: Uncategorized | Status: new
Component: Documentation | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Please,
Could you add an example for formfield_for_foreignkey (
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey)
showing how to access parent ID for filtering a foreignkey.

I saw on http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-
django-admin
parent_id = request.META['PATH_INFO'].strip('/').split('/')[-1]

Clearly it's not ideal.
Thanks!

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

Django

unread,
Dec 2, 2013, 10:00:11 AM12/2/13
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------+--------------------------------------

Reporter: anonymous | Owner: nobody
Type: Uncategorized | Status: new
Component: Documentation | Version: master
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 aaugustin):

* needs_docs: => 0
* needs_tests: => 0
* needs_better_patch: => 0


Old description:

> Please,
> Could you add an example for formfield_for_foreignkey (
> https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey)
> showing how to access parent ID for filtering a foreignkey.
>
> I saw on http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-
> django-admin
> parent_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
>
> Clearly it's not ideal.
> Thanks!

New description:

Please,
Could you add an example for formfield_for_foreignkey (
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey)
showing how to access parent ID for filtering a foreignkey.

I saw on http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-
django-admin
{{{
parent_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
}}}
Clearly it's not ideal.

Thanks!

--

Comment:

Could you explain a bit more what you're trying to achieve?

To help you, we need something like: "here's my models definition, here's
my admin definiton, at this URL ... I would like to..."

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

Django

unread,
Dec 5, 2013, 7:51:29 AM12/5/13
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
--------------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timo):

* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

The request is a documentation additional as outlined in the blog post.

It would be nice if there were a cleaner way to achieve the filtering, but
until then, I guess we can document this solution.

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

Django

unread,
Feb 26, 2016, 2:27:24 PM2/26/16
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
--------------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/5846 PR]

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

Django

unread,
Mar 1, 2016, 9:48:35 AM3/1/16
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
--------------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


Comment:

Left comments for improvement.

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

Django

unread,
Apr 26, 2019, 5:22:27 AM4/26/19
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
--------------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tobias Kunze):

For reference, the blog post referred to in the original issue can be
found
[https://web.archive.org/web/20130403060642/http://www.stereoplex.com/blog
/filtering-dropdown-lists-in-the-django-admin/ on the web archive], and
the relevant code reads like this:


{{{
class MountaineeringInfoInline(admin.TabularInline):
model = MountaineeringInfo
formset = MountaineeringInfoInlineFormset

def formfield_for_dbfield(self, field, **kwargs):
if field.name == 'area':
# Note - get_object hasn't been defined yet
parent_trip = self.get_object(kwargs['request'], Trip)
contained_areas =
Area.objects.filter(area__contains=parent_trip.area.area)
return forms.ModelChoiceField(queryset=contained_areas)
return super(MountaineeringInfoInline,
self).formfield_for_dbfield(field, **kwargs)

def get_object(self, request, model):
object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
try:
object_id = int(object_id)
except ValueError:
return None
return model.objects.get(pk=object_id)
}}}

At least mentioning `formfield_for_dbfield` in the docs at all, anywhere,
sounds like a good idea to me, regardless of the specifics on how to get
the current pk or instance.

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

Django

unread,
Nov 1, 2019, 9:37:22 AM11/1/19
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Caio Ariede):

* owner: nobody => Caio Ariede
* status: new => assigned


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

Django

unread,
Nov 18, 2019, 8:57:27 AM11/18/19
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Caio Ariede):

* needs_better_patch: 1 => 0


Comment:

[https://github.com/django/django/pull/12101 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:7>

Django

unread,
Dec 19, 2019, 5:39:41 AM12/19/19
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:8>

Django

unread,
Jul 9, 2020, 2:49:09 PM7/9/20
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 1 => 0


Comment:

[https://github.com/django/django/pull/13171 New PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:9>

Django

unread,
Jul 10, 2020, 1:51:25 AM7/10/20
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:10>

Django

unread,
Jul 13, 2020, 4:56:25 PM7/13/20
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:11>

Django

unread,
Jul 13, 2020, 5:07:03 PM7/13/20
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"d38c34119e91a533c797098f150abe99b5ee2fd8" d38c3411]:
{{{
#!CommitTicketReference repository=""
revision="d38c34119e91a533c797098f150abe99b5ee2fd8"
Fixed #21528 -- Added note about filtering form field's queryset based on
instance to admin docs.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:12>

Django

unread,
Jul 13, 2020, 5:07:30 PM7/13/20
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: closed
Component: Documentation | Version: master

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"7fe7d832950bed300a9ea7a72ed05664d2037c55" 7fe7d832]:
{{{
#!CommitTicketReference repository=""
revision="7fe7d832950bed300a9ea7a72ed05664d2037c55"
[3.1.x] Fixed #21528 -- Added note about filtering form field's queryset


based on instance to admin docs.

Backport of d38c34119e91a533c797098f150abe99b5ee2fd8 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:13>

Django

unread,
Jul 13, 2020, 5:08:12 PM7/13/20
to django-...@googlegroups.com
#21528: improve Django Doc with an example for formfield_for_foreignkey accessing
parent ID
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Caio
Type: | Ariede
Cleanup/optimization | Status: closed
Component: Documentation | Version: master

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"96b04f53c56d912b491cd06a3ec776e1195c6308" 96b04f5]:
{{{
#!CommitTicketReference repository=""
revision="96b04f53c56d912b491cd06a3ec776e1195c6308"
[3.0.x] Fixed #21528 -- Added note about filtering form field's queryset


based on instance to admin docs.

Backport of d38c34119e91a533c797098f150abe99b5ee2fd8 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21528#comment:14>

Reply all
Reply to author
Forward
0 new messages