#4091: Not able to get choices widget to auto selected="selected" on
model_for_instance
-------------------------------------------+--------------------------------
   Reporter:  [EMAIL PROTECTED]                 |                Owner:  nobody 
        
     Status:  closed                       |            Component:  
django.newforms
    Version:  0.96                         |           Resolution:  worksforme  
   
   Keywords:  foreignkey selected choices  |                Stage:  Accepted    
   
  Has_patch:  0                            |           Needs_docs:  0           
   
Needs_tests:  0                            |   Needs_better_patch:  0           
   
-------------------------------------------+--------------------------------
Changes (by PhiR):

  * status:  new => closed
  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0
  * resolution:  => worksforme
  * stage:  Unreviewed => Accepted

Comment:

 I used the following model:
 {{{
 from django.db import models
 from django import newforms as forms
 
 # Create your models here.
 class ZoneGeographique(models.Model):
         nom = models.CharField(maxlength=256)
 
         def __str__(self):
                 return self.nom
 
         class Admin:
                 pass
 class Ville(models.Model):
         pays = models.ForeignKey(ZoneGeographique)
         nom = models.CharField(maxlength=256)
 
         def __str__(self):
                 return self.nom + " en " + self.pays.nom
 
         class Admin:
                 pass
 
 }}}
 
 Then from the django shell:
 {{{
 >>> from test_form.models import *
 >>> fr = ZoneGeographique(nom='France')
 >>> fr.save()
 >>> de = ZoneGeographique(nom='Allemagne')
 >>> de.save()
 >>> paris = Ville(pays=fr, nom='Paris')
 >>> paris.save()
 >>> paris
 <Ville: Paris en France>
 >>> import django.newforms as forms
 >>> ParisForm = forms.form_for_instance(paris)
 >>> print ParisForm()
 <tr><th><label for="id_pays">Pays:</label></th><td><select name="pays"
 id="id_pays">
 <option value="">---------</option>
 <option value="1" selected="selected">France</option>
 <option value="2">Allemagne</option>
 </select></td></tr>
 <tr><th><label for="id_nom">Nom:</label></th><td><input id="id_nom"
 type="text" name="nom" value="Paris" maxlength="256" /></td></tr>
 }}}
 
 The right instance is selected. It works

-- 
Ticket URL: <http://code.djangoproject.com/ticket/4091#comment:1>
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