0

我在我的通知模型中创建了一个 simple_history.models.HistoricalRecords 实例

模型.py

class Notification(models.Model):
    title = models.CharField(max_length=500)
    content = RichTextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    history = HistoricalRecords()


class Attachment(models.Model):
    title = models.CharField(max_length=500)
    attachement = models.FileField(upload_to = user_directory_path)
    notifiaction = models.ForeignKey(Notification, on_delete=models.SET_NULL, null= True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

架构.py

class AttachmentNode(DjangoObjectType):
    class Meta:
        model = Attachment
        filter_fields = ['title']
        interfaces = (relay.Node, )


class NotificationNode(DjangoObjectType):
    class Meta:
        model = Notification
        filter_fields = {
            'id': ['exact'],
            'title': ['exact', 'icontains', 'istartswith'],
        }
        interfaces = (relay.Node, )



class Query(graphene.ObjectType):
    notifications = relay.Node.Field(NotificationNode)
    all_notifications = DjangoFilterConnectionField(NotificationNode)

这工作正常,但是当我查询通知时,我想要由 HistoricalRecords() 为 graphql 端点中的通知模型创建的所有记录。我怎样才能做到这一点?

4

1 回答 1

1

我假设您希望其中包含每个通知的历史记录,可能会在您的应用程序的某个位置显示它。

从修订列表中获取更改列表

看起来您必须首先解决一个问题 - django-simple-history 存储对象的修订版,而不是差异。你会想出一种方法来转换它

[
    <HistoricalNotification: Notification object as of 2010-10-25 18:04:13.814128>,
    <HistoricalNotification: Notification object as of 2010-10-25 18:03:29.855689>,
]

通过比较相邻的修订版并找到差异来达到类似的效果:

[
    "Created by Jack Pear 10 minutes ago",
    "title changed by Jack Pear 8 minutes ago",
]

实际上,您可能需要 dicts 而不是 stings,因此在前端处理起来更容易。

django-simple-history 中包含一个工具。下面是一些伪代码,你可以如何做到这一点:

class Notification(models.Model):
    title = models.CharField(max_length=500)
    content = RichTextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    history = HistoricalRecords()

    def get_changelist(self):
        record = self.history.first()
        while record.next_record:
            delta = record.diff_against(record.next_record)
            for change in delta.changes:
                yield {
                    "field": change.field,
                    "old": change.old,
                    "new": change.new,
                    "changed_by": record.next_record.history_user,
                    "changed_at": record.next_record.history_date,
                }
            record = record.next_record

将更改列表添加到 graphql

我们必须为更改列表对象声明一个单独的 ObjectType:

class ChangeListItemNode(graphene.ObjectType):
    field = graphene.ID()
    old = graphene.ID()
    new = graphene.ID()
    changed_by = graphene.Field(UserNode)
    changed_at = graphene.DateTime()

之后,我们声明一个带有解析器的自定义字段NotificationNode

class NotificationNode(DjangoObjectType):
    changelist = graphene.List(ChangeListItemNode)

    class Meta:
        model = Notification
        filter_fields = {
            'id': ['exact'],
            'title': ['exact', 'icontains', 'istartswith'],
        }
        interfaces = (relay.Node, )

    @staticmethod
    def resolve_changelist(notification, info, **kwargs):
        return notification.get_changelist()

请记住,这没有经过测试,它只是伪代码并且肯定包含错误。不过,这应该提供一个起点。

于 2022-01-10T10:43:56.933 回答