Django BooleanField作为Inlineadmin的单选按钮

时间:2022-04-16 13:49:27

I couldn't find a street way to do it.

我找不到一条路来做这件事。

2 个解决方案

#1


1  

I couldn't find a street way to do it, so I make tweak with JavaScript to do it.

我找不到街头的方法来做这件事,所以我用JavaScript做了一些调整。

$(document).ready(function() {
  $(".field-current").live("click",function(){

    $action = $($(this).children()[0])
    current_state = $action.is(':checked');
    $('.field-current').each(function(index) {
      var $temp = $($(this).children()[0])
      $temp.val(false);  
      $temp.attr('checked', false);
    });

    var $current = $($(this).children()[0])
    if (current_state == "on" || current_state == true){  
      $current.val(true);
      $current.attr('checked', true);
    }else{
      $current.val(false);
      $current.attr('checked', false);
    }
  });
});

in the admin page add

在管理页面中添加

class ArticleDetailsInlineAdmin(admin.TabularInline):
     ....
    fields     = ['current', 'slug','summary','mod_date']

class ArticleHeaderAdmin(admin.ModelAdmin):
    inlines = [ArticleDetailsInlineAdmin,]
    ......
    form = ArticleForm

class Media:
        js = ('js/admin-current-article.js')

by doing this the current field will act like radio button among the inlineadmin

通过这样做,当前字段将在inlineadmin中充当单选按钮

#2


1  

Try using the formfield_overrides property to change the widget used (it's available on InlineAdmin classes as well as ModelAdmin):

尝试使用formfield_overrides属性来更改所使用的小部件(它可以在InlineAdmin类和ModelAdmin中使用):

from django.forms.widgets import RadioSelect

class ArticleDetailsInlineAdmin(admin.TabularInline):
    ...
    fields = ['current', 'slug', 'summary', 'mod_date']
    formfield_overrides = {
            models.BooleanField: {'widget': RadioSelect},
        }

Note this will use radio buttons instead of checkboxes for all BooleanFields in ArticleDetailsInlineAdmin.

注意,对于ArticleDetailsInlineAdmin中的所有booleanfield,这将使用单选按钮而不是复选框。

#1


1  

I couldn't find a street way to do it, so I make tweak with JavaScript to do it.

我找不到街头的方法来做这件事,所以我用JavaScript做了一些调整。

$(document).ready(function() {
  $(".field-current").live("click",function(){

    $action = $($(this).children()[0])
    current_state = $action.is(':checked');
    $('.field-current').each(function(index) {
      var $temp = $($(this).children()[0])
      $temp.val(false);  
      $temp.attr('checked', false);
    });

    var $current = $($(this).children()[0])
    if (current_state == "on" || current_state == true){  
      $current.val(true);
      $current.attr('checked', true);
    }else{
      $current.val(false);
      $current.attr('checked', false);
    }
  });
});

in the admin page add

在管理页面中添加

class ArticleDetailsInlineAdmin(admin.TabularInline):
     ....
    fields     = ['current', 'slug','summary','mod_date']

class ArticleHeaderAdmin(admin.ModelAdmin):
    inlines = [ArticleDetailsInlineAdmin,]
    ......
    form = ArticleForm

class Media:
        js = ('js/admin-current-article.js')

by doing this the current field will act like radio button among the inlineadmin

通过这样做,当前字段将在inlineadmin中充当单选按钮

#2


1  

Try using the formfield_overrides property to change the widget used (it's available on InlineAdmin classes as well as ModelAdmin):

尝试使用formfield_overrides属性来更改所使用的小部件(它可以在InlineAdmin类和ModelAdmin中使用):

from django.forms.widgets import RadioSelect

class ArticleDetailsInlineAdmin(admin.TabularInline):
    ...
    fields = ['current', 'slug', 'summary', 'mod_date']
    formfield_overrides = {
            models.BooleanField: {'widget': RadioSelect},
        }

Note this will use radio buttons instead of checkboxes for all BooleanFields in ArticleDetailsInlineAdmin.

注意,对于ArticleDetailsInlineAdmin中的所有booleanfield,这将使用单选按钮而不是复选框。