2009-09-16 48 views
23

这里是我的models.py的一部分:在Django管理在线选择项目与单选按钮

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) 

我想PERSONNAME可编辑内嵌的人在管理作为TabularInline。此外,我希望PersonName的default_name字段显示为单选按钮,以便每个Person只有一个PersonName可以具有default_name = True。

这就是最后一部分的难点。在Django中,在表单中有一个单选按钮接口相对比较简单。有一个基本上选择一个formset中的一个表单,似乎要难得多。要做到这一点

一种方法是添加JavaScript到使用正则表达式匹配<输入DEFAULT_NAME >元素,并确保检查他们中的一个取消选中其余change_form.html的人。但是,如果Django命名HTML表单域的约定发生变化,这看起来好像可能会被破坏;此外,它只能解决启用了JavaScript的浏览器的用户的问题。

感谢您收到任何更好的点子!

UPDATE 17/9/2009

以下管理模板扩展(基于thisthis)执行JavaScript的建议之上,但更好的解决方案将受到欢迎!

{% 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 %} 

回答

1

我认为你不会得到一个传统的纯HTML方法来做你想做的事情。

虽然你不应该依赖这里的正则表达式。你能不能添加一个类名的输入字段,以便jQuery的...

我想这是难度比我首先想到的(在这里找到答案): Define css class in django Forms

这只是一个稍微好一点的解决方案虽然。