我使用以下模型,其中默认的django用户表与酒店的表具有多对多关系。
在django管理面板中将多个用户分配给酒店非常困难,我想将默认的“ ctrl-click”方法替换为一个复选框,在这里我可以选择多个用户而无需按住ctrl。
型号:
from django.db import models
from django.contrib.auth.models import User
class Hotel(models.Model):
# associate the user
user = models.ManyToManyField(User)
# additional fields
hotel_name = models.CharField(max_length=100)
hotel_id = models.CharField(max_length=100)
def __str__(self):
return self.hotel_id
admin.py
from django.contrib import admin
from .models import *
from django.forms import CheckboxSelectMultiple
# Checkbox for many-to-many fields
class HotelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}
admin.site.register(Hotel)
答案 0 :(得分:0)
您在register
方法中的语法不正确:
class HotelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}
admin.site.register(Hotel, HotelAdmin)