【问题标题】:Django contenttypes how to get foreign key valuesDjango contenttypes如何获取外键值
【发布时间】:2012-11-20 12:11:25
【问题描述】:

我正在开发项目管理工具以提高我的 django 技能。 我的内容类型有问题。

我有下一个模型:
项目
票证 - 具有项目的外键
讨论 - 有项目的外键
评论 - 有讨论的外键
UserProfile - django 用户的扩展
ProfileWatch - 用户观看的项目或讨论,内容类型在此处使用
ProfileActions - 包含用户操作(添加评论到讨论、开始讨论、添加票证)内容类型也在这里使用。此表中的记录由来自工单、讨论和评论的信号创建

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

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

理想情况下,在模板中我需要这样的东西(在括号字段中ProfileAction 模型):
13:55 19.11.2012(action_date) admin(profile) add ticket(action_type) deploy this(?) to 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
        )

【问题讨论】:

    标签: django django-orm django-contenttypes


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多