我有一个想要内联的 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.")
以及项目的模型代码:
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()
以及视图的管理代码:
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 = [UserProfileInline,]
admin.site.register(Project, ProjectAdmin)