Opened 17 years ago

Closed 11 years ago

Last modified 11 years ago

#4467 closed New feature (wontfix)

handle a huge list of ForeignKey objects by implementing a CategorizedModelChoiceField

Reported by: ulda Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords: ForeignKey ModelChoiceField
Cc: ulf.dambacher@… Triage Stage: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


Think of having a model like this:

    ('MF', 'Metrisch-Fein'),
#   some more here left out for shortness...
class Profil(models.Model):

and your database fills up with profile objects in hundreds with different categories.

Now another model is calling this model like

class Karte(models.Model):


and in your forms for Karte you get an ugly select filling the screen.

Attached you find a CategorizedModelChoiceField. With this in hand, you subclass ForeignKey to your model specific ProfileKey

class ProfilKey(models.ForeignKey):
    def formfield(self,**kwargs):
        defaults = {'form_class': CategorizedModelChoiceField, 'queryset':, 'category': 'kategorie' , 'callback_link': '/pr/list_of_profiles_for_category/'}
        return super(ProfilKey, self).formfield(**defaults)

and change the ForeignKey definition in your other model to


Because the whole thing works with a simple kind of ajax, you need a callback function in your urls like this:

urlpatterns = patterns('myproject.categorizedmodelchoicefield',
     { 'queryset': Profil.objects.all(),
       'category_name':'kategorie' } ),

and voilais: in your forms for Karte you get two selects for category and the actual choice value which updates according to the users selection.

you may also use the formfield_callback but why not using the objects if they are handy...

making it work you need:

  • attached
  • JsonResponse from
  • http.Request form loaded somewhere in your base templates

I think something like this should be included as example extension to newforms and to using django/json/ajax. therefore i didn't post ant djangosnipplets.

the javascript code maybe looks ugly, but this is because it was the first time i did javascript.

Attachments (1) (4.8 KB ) - added by ulda <ulf.dambacher@…> 17 years ago.

Download all attachments as: .zip

Change History (7)

by ulda <ulf.dambacher@…>, 17 years ago

comment:1 by James Bennett, 17 years ago

Patch needs improvement: set

The JS in this patch seems to require some third-party toolkit to work properly; tying Django to a JavaScript library is not something we're interested in doing.

comment:2 by Jacob, 16 years ago

Triage Stage: UnreviewedSomeday/Maybe

comment:3 by Gabriel Hurley, 13 years ago

Severity: Normal
Type: New feature

comment:4 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 by Aymeric Augustin, 11 years ago

Resolution: wontfix
Status: newclosed

The implementation is very far from Django's current standards, and it doesn't need to live in core.

Besides, demand for this feature is non-existent: there hasn't been a single comment here in 6 years.

Last edited 11 years ago by Aymeric Augustin (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top