2012-09-13 50 views
0

我有兩個表被告和Defendant_Potential擴展Django管理tabular.html定製複選框行動

class Defendant(models.Model): 
    fullname   = models.CharField(max_length = 100, null = False,) 
    fname   = models.CharField(max_length = 100, verbose_name = u'First Name', blank = True, null = True,) 
    midname   = models.CharField(max_length = 100, verbose_name = u'Middle Name', blank = True, null = True,) 
    lname   = models.CharField(max_length = 50, verbose_name = u'Last Name', null = True, blank = True,) 
    marked   = models.CharField(max_length = 50, verbose_name = u'Check box mark', null = True, blank = True,) 
class Defendant_Potential(models.Model): 
    defendant = models.ForeignKey('Defendant', related_name = "Defendant_defendantID",) 
    midname  = models.CharField(max_length = 100, blank = True, null = True, default = None) 
    address  = models.CharField(max_length = 100, null = False,) 
    city   = models.CharField(max_length = 50, null = False,) 
    state  = models.CharField(max_length = 5, null = False,) 
    zip  = models.CharField(max_length = 50, null = False,) 

而且它的管理模型,如:

class Defendant_Potential_Individual_Inline(admin.TabularInline): 
    model = Defendant_Potential 

    formset = NoDeleteAdminInlineFormSet 
    readonly_fields = ('midname', 'address') 
    extra = 0 
    template = "admin/cdradmin/defendant/edit_inline/tabular.html" 

class DefendantAdmin(admin.ModelAdmin): 
    list_display = ('fullname', 'fname','midname','lname') 

    inlines = [Defendant_Potential_Individual_Inline, ] 

在tabular.html我有一個自定義的複選框

<td><input type="checkbox" name="defendant_potential_checked" data-defendant_id="{{ inline_admin_form.original.defendant_id }}" value="{{ inline_admin_form.original.id }}"></input></td> 

我的枯竭是無論何時勾選複選框,然後'被告'表會更新標記的字段(這應該發生olny我們單擊change_from.html上的提交按鈕。

我做了一些關於save_model,save_formset等的研究但我沒有得到正確的解決方案任何人都可以幫助我做到這一點?

+0

如果標記比爲什麼不創建爲布爾字段一個複選框場?布爾字段成爲ModelForm上的複選框。我想硬編碼模板上的複選框字段是不正確的做法。 – Eduardo

+0

標記的字段在被告表中。當用戶選中複選框時。這個字段應該填充。 –

回答

0

假設你有型號:

class Hive(models.Model): 
    name = models.CharField(max_length=100) 
    marker = models.BooleanField(default=False) 


class Bee(models.Model): 
    hive = models.ForeignKey(Hive) 
    name = models.CharField(max_length=100) 

那麼你的管理看起來是這樣的:

class BeeForm(forms.ModelForm): 
    check = forms.BooleanField(label="check", required=False) 
    class Meta: 
     model = Bee 

class BeeInline(admin.TabularInline): 
    form = BeeForm 
    model = Bee 

class HiveAdmin(admin.ModelAdmin): 
    inlines = (BeeInline,) 
    def save_formset(self, request, form, formset, change): 
     for bee_form in formset: 
      if bee_form.cleaned_data.get('check') == True: 
       form.instance.marker = True 
       form.instance.save() 
     super(HiveAdmin, self).save_formset(request, form, formset, change) 

admin.site.register(Hive, HiveAdmin) 
+0

這幫助我解決我的問題,謝謝@http://stackoverflow.com/users/1647273/christian-thieme –