Opened 11 years ago

Closed 4 years ago

Last modified 4 years ago

#21528 closed Cleanup/optimization (fixed)

improve Django Doc with an example for formfield_for_foreignkey accessing parent ID

Reported by: anonymous Owned by: Caio Ariede
Component: Documentation Version: dev
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 (last modified by Aymeric Augustin)

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!

Change History (14)

comment:1 by Aymeric Augustin, 11 years ago

Description: modified (diff)

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..."

comment:2 by Tim Graham, 11 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

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.

comment:3 by Tim Graham, 9 years ago

Has patch: set

comment:4 by Tim Graham, 9 years ago

Patch needs improvement: set

Left comments for improvement.

comment:5 by Tobias Kunze, 6 years ago

For reference, the blog post referred to in the original issue can be found 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.

comment:6 by Caio Ariede, 5 years ago

Owner: changed from nobody to Caio Ariede
Status: newassigned

comment:7 by Caio Ariede, 5 years ago

Patch needs improvement: unset

comment:8 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:9 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset

comment:10 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:11 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:12 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In d38c3411:

Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.

comment:13 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 7fe7d832:

[3.1.x] Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.

Backport of d38c34119e91a533c797098f150abe99b5ee2fd8 from master

comment:14 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 96b04f5:

[3.0.x] Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.

Backport of d38c34119e91a533c797098f150abe99b5ee2fd8 from master

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