Django contenttypes如何获取外键值

时间:2012-11-20 12:11:26

标签: django django-orm django-contenttypes

我正在研究项目管理工具,以提高我的django技能。 我对contenttypes有疑问
我有下一个型号:
项目
机票 - 具有ForeignKey到项目
讨论 - 对项目有异议 评论 - 有ForeignKey讨论
UserProfile - django用户的扩展名 ProfileWatch - 用户观看的项目或讨论,contenttype在这里使用
ProfileActions - 包含用户操作(添加评论到讨论,开始讨论,添加票证)contenttype也在这里使用。此表中的记录由来自故障单,讨论和评论的信号创建

用户收到有关他观看的内容的通知(有人留下新评论或开始讨论或添加票证)。 在视图中,我收到当前用户的所有通知。

我知道对象(项目,讨论)有用户操作,但我不知道哪个对象触发此UserAction(票证,评论,讨论)。

理想情况下,在模板中我需要这样的东西(在ProfileAction模型的括号字段中):
13:55 19.11.2012(action_date)admin(profile)add ticket(action_type)将此(?)部署到JustTestProject(content_object)

但现在我有了这个:
13:55 19.11.2012(action_date)admin(profile)将票证(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
        )

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。只需将下一个附加字段添加到Profile Action模型并更改信号处理程序。

trigger_content_type = models.ForeignKey(ContentType, related_name='triggers')
trigger_id = models.PositiveIntegerField()
trigger_object = generic.GenericForeignKey('trigger_content_type', 'trigger_id')