【发布时间】:2012-02-23 08:28:03
【问题描述】:
this question 上的一些 cmets 表示有更直接和最新的解决方案。 (没找到)
(cmets 喜欢:“是的,它已经解决了。只需在参数中传递相关实例,它就会预先填充它......”让我觉得现在很容易,但我仍然无法让它工作)
问题是我想向一些参与给定事件并由管理员选择的用户发送通知。 工作人员在事件页面上,选择事件并保存它,然后 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)
【问题讨论】:
-
为什么不通过在用户模型上创建一个过滤器来过滤所有收到(或未收到)通知的用户来解决这个问题?然后您可以创建一个发送通知的管理操作。
-
步骤是:过滤与给定戏剧有关系的用户。而不是让工作人员指出他们中的哪些人将收到给定重演(事件)的通知 - 并非所有这些都是必要的(例如,导演没有参加所有重演)。此操作是在工作人员提交有关此特定事件的所有其他信息时完成的。
-
这样就清楚多了。那么为什么不通过将多对多事件链接到用户来解决它呢?它可以是像filter_horizontal 或inline 这样的小部件。然后,您可以向这些用户发送通知。
-
我试图减少问题以使问题清晰,但这并不是那么简单。用户不是直接 M-M 到事件。有很多中间模型,如“角色”、“角色名称”等,它们在关系中生效。剧院环境相当复杂,有时甚至是混乱的。我想让非技术人员尽可能轻松地填写我需要的所有信息:电子邮件通知、网页生成、为所有相关人员定制的时间表……现在我离我的圣杯;-)
-
除非您明确说明您想要什么,否则我们无法真正帮助您。
标签: django django-admin inline