this question的一些注释表明存在更直接和最新的解决方案。 (我没有找到任何)Django Admin:如何根据父模型“预填充”内联外键字段
(评论:“是的,它解决了。只需传递参数中的相关实例,它将预先填充它...”让我觉得现在很容易,但我仍然可以不要让它工作)
事情是,我想发送通知给某些用户参与特定事件,并由管理员选定。 工作人员在事件的页面上,选择事件并保存它,比inline中的fk字段应该预先填充所涉及的用户名。 (并等待工作人员检查它们或取消选中它们以接收电子邮件通知)
由于可用性的原因,这是重要的,这应该采取内联的形式,因为有很多页面已经准备好收集所有必要的信息来自员工的信息。
任何想法?
class When(models.Model):
...
Date = models.DateTimeField(unique=True)
Nameofevent = models.ForeignKey(Event, to_field='Name') #the Event model gets then username from models.ManyToManyField(User, through='Role')
...
def getteam(self):
teamlist = self.Nameofevent.Roleinevent.all() # gathering users which are involved
return teamlist
getteamresult = property(getteam)
class Notice(models.Model): #this will go to the inline
...
Who = models.ForeignKey(User, blank=True)
To_notice_or_not_to_notice = models.BooleanField(default=False)
...
class NoticeInline(admin.TabularInline):
model = Notice
extra = 9
class WhenAdmin(admin.ModelAdmin):
list_display = ('Date', ...)
readonly_fields = ('getteamresult',) #this is to make clear i have access to what i want and i can display it. prints out as [<User: someone>, <User: someoneelse>]
inlines = [NoticeInline] #but i want the User objects to prepopulate foreign field here in inline model
admin.site.register(When,WhenAdmin)
为什么不解决它通过创建过滤所有收到(或未收到)通知的用户的用户模型?然后,您可以创建发送通知的管理操作。 – 2012-01-31 17:35:27
这些步骤是:过滤与用户剧场戏有关的用户。让工作人员指出哪些人会收到关于重新发布(事件)的通知 - 并非所有人都是必要的(例如,导演没有参加所有报复)。此操作在员工提交有关此特定事件的所有其他信息时完成。 – tookanstoken 2012-02-01 01:36:34
这使得它更清晰。那么为什么不通过在与用户相关的事件中有多对多来解决呢?它可以是[filter_horizontal](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal)或[内嵌](https: //docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-models)。然后,您可以向这些用户发送通知。 – 2012-02-01 05:26:36