我正在處理項目管理工具,以提高我的Django技能。 我有一個內容類型的問題。
我旁邊型號:
項目
票務 - 已ForeignKey的到項目
討論 - 有ForeignKey的到項目
評論 - 有ForeignKey的,以討論
用戶配置 - Django的用戶的擴展
ProfileWatch - 項目或討論用戶觀看的內容類型
ProfileActions - 包含用戶操作(爲討論添加評論,開始討論,添加故障單)contenttype也可在此處使用。討論和評論中的信號創建的表格中的記錄
Django contenttypes如何獲得外鍵值
用戶收到有關他觀看的事情(有人留下新評論或開始討論或添加故障單)的通知。 在視圖中,我得到當前用戶的所有通知。
我知道哪些對象(項目,討論)有用戶操作,但我不知道哪個對象觸發此UserAction(Ticket,Comment,Discussion)。
理想情況下,我需要類似這樣的內容(在ProfileAction模型的括號字段中):
13:55 19.11.2012(action_date)admin(profile)add ticket(action_type)將此(?)部署到JustTestProject(content_object)
但現在我有這樣的:
13:55二〇一二年十一月一十九日(ACTION_DATE)管理員(配置文件)添加票(ACTION_TYPE)到JustTestProject(content_object)有關如何安排店面觸發對象模型
任何想法?
感謝所有幫助
查看:
from django.views.generic import ListView
from app.models import ProfileAction
class ActionsList(ListView):
context_object_name = "actions"
template_name = 'index.html'
def get_queryset(self):
profile = self.request.user.get_profile()
where = ['(content_type_id={0} AND object_id={1})'.format(\
x.content_type_id,\
x.object_id\
) for x in profile.profilewatch_set.all()\
]
recent_actions = ProfileAction.objects.extra(
where=[
' OR '.join(where),
'profile_id={0}'.format(profile.pk)
],
order_by=['-action_date']
)
return recent_actions
型號:
#models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
class UserProfile(models.Model):
user = models.OneToOneField(User, verbose_name=_("Django user"))
first_name = models.CharField(_("First name"), blank=True, max_length=64, null=True)
last_name = models.CharField(_("Last name"), blank=True, max_length=64, null=True)
info = models.TextField(_("Additional information"), null=True)
phone = models.CharField(verbose_name=_("Phone"), max_length=15, blank=True, null=True)
class ProfileWatch(models.Model):
profile = models.ForeignKey(to=UserProfile, verbose_name=_(u"User profile"))
start_date = models.DateTimeField(verbose_name=_(u"Start date"), auto_now_add=True)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class ProfileAction(models.Model):
ACTION_TYPE_CHOICES = (
(0, _(u"Add comment")),
(1, _(u"Add discussion")),
(2, _(u"Add ticket")),
)
profile = models.ForeignKey(to=UserProfile, verbose_name=_(u"User profile"))
action_date = models.DateTimeField(verbose_name=_(u"Start date"), auto_now_add=True)
action_type = models.PositiveSmallIntegerField(
verbose_name=_("Status"),
choices=ACTION_TYPE_CHOICES
)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Project(models.Model):
title = models.CharField(_("Project title"), max_length=128)
description = models.TextField(_("Project description"), blank=True, null=True)
members = models.ManyToManyField(UserProfile, through='Participation', verbose_name=_("Members"), blank=True, null=True)
actions = generic.GenericRelation(ProfileAction)
class Ticket(models.Model):
title = models.CharField(_("Title"), max_length=256)
project = models.ForeignKey('Project', verbose_name=_("Project"))
description = models.TextField(_("Ticket description"), blank=True, null=True)
creator = models.ForeignKey(UserProfile, verbose_name=_("Creator"), related_name='created_tickets')
@receiver(post_save, sender=Ticket)
def add_action_for_ticket(sender, instance, created, **kwargs):
if created:
ProfileAction.objects.create(
profile=instance.creator,
action_type=2,
content_object=instance.project
)
class Discussion(models.Model):
project = models.ForeignKey(
to=Project,
verbose_name=_(u"Project"),
)
creator = models.ForeignKey(
to=UserProfile,
verbose_name=_(u"Creator"),
)
updated = models.DateTimeField(
verbose_name=_("Last update"),
auto_now=True
)
title = models.CharField(
verbose_name=_(u"title"),
max_length=120
)
actions = generic.GenericRelation(ProfileAction)
@receiver(post_save, sender=Discussion)
def add_action_for_discussion(sender, instance, created, **kwargs):
if created:
ProfileAction.objects.create(
profile=instance.creator,
action_type=1,
content_object=instance.project
)
class Comment(models.Model):
discussion = models.ForeignKey(
to=Discussion,
verbose_name=_(u"Discussion")
)
creator = models.ForeignKey(
to=UserProfile,
verbose_name=_(u"Creator"),
)
pub_date = models.DateTimeField(
verbose_name=_("Publication date"),
auto_now_add=True
)
text = models.TextField(
verbose_name=_("Comment text")
)
@receiver(post_save, sender=Comment)
def add_action_for_comment(sender, instance, created, **kwargs):
if created:
ProfileAction.objects.create(
profile=instance.creator,
action_type=0,
content_object=instance.discussion
)