#4467: handle a huge list of ForeignKey objects by implementing a
CategorizedModelChoiceField
-----------------------+----------------------------------------------------
Reporter:  ulda        |       Owner:  adrian                     
  Status:  new         |   Component:  django.newforms            
 Version:  SVN         |    Keywords:  ForeignKey ModelChoiceField
   Stage:  Unreviewed  |   Has_patch:  0                          
-----------------------+----------------------------------------------------
 Think of having a model like this:
 {{{
 PRKAT_CHOICES= (
     ('M','Metrisch'),
     ('MF', 'Metrisch-Fein'),
 #   some more here left out for shortness...
 )
 class Profil(models.Model):
 kategorie=models.CharField('Kategorie',maxlength=4,choices=PRKAT_CHOICES)
     bezeichnung=models.CharField('Bezeichnung',maxlength=30)
 
 }}}
 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):
         ba_nr=models.PositiveIntegerField("BA-Nr",primary_key=True)
         datum=models.DateField("Datum",default=date.today)
 
         profil=ForeignKey(Profil)
 }}}
 
 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':
 self.rel.to._default_manager.all(), 'category': 'kategorie' ,
 'callback_link': '/pr/list_of_profiles_for_category/'}
         defaults.update(kwargs)
         return super(ProfilKey, self).formfield(**defaults)
 
 }}}
 
 and change the ForeignKey  definition in your other model to
 {{{
         profil=ProfileKey(Profil)
 }}}
 
 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',
     (r'^pr/list_of_profiles_for_category/$','get_choices_for_category',
      { '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:
   * CategorizedModelChoiceField.py attached
   * JsonResponse from djangosnipplets.org
   * http.Request form openjsan.org 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.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/4467>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to