我有一个Django模型字段,我想内联.该领域是一种多对多的关系.所以有"项目"和"用户档案".每个用户配置文件可以选择任意数量的项目.
目前,我有"表格式"内联视图工作.有没有办法有一个"水平过滤器",以便我可以轻松地添加和删除用户配置文件中的项目?
请参阅附图以获取示例.
这是用户配置文件的型号代码:
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
projects = models.ManyToManyField(Project, blank=True, help_text="Select the projects that this user is currently working on.")
Run Code Online (Sandbox Code Playgroud)
和项目的模型代码:
class Project(models.Model):
name = models.CharField(max_length=100, unique=True)
application_identifier = models.CharField(max_length=100)
type = models.IntegerField(choices=ProjectType)
account = models.ForeignKey(Account)
principle_investigator = models.ForeignKey(User)
active = models.BooleanField()
Run Code Online (Sandbox Code Playgroud)
以及视图的管理代码:
class UserProfileInline(admin.TabularInline):
model = UserProfile.projects.through
extra = 0
verbose_name = 'user'
verbose_name_plural = 'users'
class ProjectAdmin(admin.ModelAdmin):
list_display = ('name', 'application_identifier', 'type', 'account', 'active')
search_fields = ('name', 'application_identifier', 'account__name')
list_filter = ('type', 'active')
inlines …Run Code Online (Sandbox Code Playgroud) 是否可以filter_horizontal用于ManyToManyField具有中间表的字段,例如没有中间表的字段?
例如:
class A(models.Model):
f1 = models.ManyToManyField(B)
f2 = models.ManyToManyField(C, through='T')
class B(models.Model):
pass
class C(models.Model):
pass
class T(models.Model):
a = models.ForeignKey(A)
c = models.ForeignKey(C)
class AAdmin(admin.ModelAdmin):
filter_horizontal = ('f1', 'f2', )
Run Code Online (Sandbox Code Playgroud)