2009-09-16 17 views
23

Oto część mojego models.py:Wybierz element w Django administratora inline z przycisków radiowych

class Person(models.Model): 
    birth_year = WideYear(null=True, blank=True) 
    birth_year_uncertain = models.BooleanField() 
    death_year = WideYear(null=True, blank=True) 
    death_year_uncertain = models.BooleanField() 
    flourit_year = WideYear(null=True, blank=True) 
    flourit_year_uncertain = models.BooleanField() 
    FLOURIT_CHOICES = (
     (u'D', u'Birth and death dates'), 
     (u'F', u'Flourit date'), 
    ) 
    use_flourit = models.CharField('Date(s) to use', max_length=2, choices=FLOURIT_CHOICES) 
    def __unicode__(self): 
     if self.personname_set.filter(default_name__exact=True): 
      name = z(self.personname_set.filter(default_name__exact=True)[0]) 
     else: 
      name = u'[Unnamed person]' 
     if self.use_flourit == u'D': 
      dates = '%s - %s' % (z(self.birth_year), z(self.death_year)) 
     else: 
      dates = 'fl. %s' % (z(self.flourit_year)) 
     return '%s (%s)' % (name, dates) 
    class Meta: 
     ordering = ['ordering_string'] 

class PersonName(models.Model): 
    titles = models.CharField(max_length=65535, null=True, blank=True) 
    surname = models.CharField(max_length=255, null=True, blank=True) 
    first_name = models.CharField(max_length=255, null=True, blank=True) 
    middle_names = models.CharField(max_length=255, null=True, blank=True) 
    post_nominals = models.CharField(max_length=65535, null=True, blank=True) 
    default_name = models.BooleanField() 
    person = models.ForeignKey(Person, null=True, blank=True) 
    def __unicode__(self): 
     return '%s, %s %s' % (self.surname, self.first_name, self.middle_names) 
    class Meta: 
     unique_together = ("titles", "surname", "first_name", "middle_names", "post_nominals", "person") 
     unique_together = ("default_name", "person") 
post_save.connect(post_save_person_and_person_name, sender=PersonName) 

Chcę PERSONNAME być edytowane inline z osobą w admin jako TabularInline. Chcę również, aby pole default_name w polu PersonName było wyświetlane jako przycisk opcji, tak aby tylko jedna nazwa osoby na osobę mogła mieć wartość nazwa_domeny = Prawda.

To ostatnia część, która jest trudna. Interfejs z przyciskami radiowymi w formularzu jest stosunkowo łatwy w Django. Posiadanie takiego, który wybiera zasadniczo jedną formę w zestawie, wydaje się znacznie trudniejsze.

Jednym ze sposobów, aby to zrobić byłoby dodać JavaScript do change_form.html dla osoby, która używa regex dopasować wejście < > elementy default_name i zapewnia, że ​​sprawdzenie jednego z nich usuwa zaznaczenie pola wyboru resztę. Ale wygląda na to, że może się zepsuć, jeśli zmieni się konwencja Django nazywania pól formularza HTML; plus, to naprawi tylko sprawy dla użytkowników z przeglądarkami obsługującymi JavaScript.

Wszelkie lepsze pomysły z wdzięcznością otrzymane!

UPDATE 17/9/2009

Poniższy administratora rozszerzenie szablonu (w oparciu o this i this) realizuje sugestię JavaScript powyżej, ale lepsze rozwiązania będzie mile widziana!

{% extends "admin/change_form.html" %} 

{% block extrahead %} 
    <script src="/site_media/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     jQuery(document).ready(function(){ 
      // Replace default_name checkboxes with radio buttons. 
      $(".default_name input").each(function(){ 
       var name = $(this).attr('name'); // grab name of original 
       var id = $(this).attr('id'); // grab id of original 
       if ($(this).attr('checked')) { // grab checked value of original 
        var checked = ' checked="checked"'; 
       } else { 
        var checked = ''; 
       } 
       /* create new visible input */ 
       var html = '<input type="radio" name="'+name+'" id="'+id+'" value="True"'+checked+' />'; 
       $(this).after(html).remove(); // add new, then remove original input 
      }); 
      // Ensure only one default_name radio button can be checked at a time. 
      $(".default_name input").click(function(){ 
       $(".default_name input").not(this).removeAttr("checked"); 
      }); 
     }); 
    </script> 
{% endblock %} 

Odpowiedz

1

Wydaje mi się, że nie otrzymasz tradycyjnej metody tylko do robienia tego, co chcesz.

Nie powinieneś jednak polegać na regexie. Nie można dodać nazwę klasy do pól wejściowych tak, że jQuery ...

myślę, że to trudniejsze niż pierwsza myśl (tu odpowiedzi): Define css class in django Forms

To tylko nieznacznie lepsze rozwiązanie chociaż.

Powiązane problemy