0

我有两个表 Defendant 和 Defendant_Potential

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>

我的Qustion是每当检查复选框时,“被告”表获得更新标记的字段(这应该发生OLNY,我们单击canse_from.html上的提交按钮。

我对 save_model、save_formset 等进行了一些研究但我没有得到任何人可以帮助我做到这一点的正确解决方案?

4

1 回答 1

2

假设您必须模型:

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)
于 2012-09-13T07:09:27.633 回答