简体   繁体   English

如何在 Graphene-Django 中传递 django-simple-history model?

[英]How to pass django-simple-history model in Graphene-Django?

I have created an instance of simple_history.models.HistoricalRecords in my Notification model我在我的通知 model 中创建了一个 simple_history.models.HistoricalRecords 实例

models.py模型.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)

schema.py架构.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)

This is working fine but I want all the records created by the HistoricalRecords() for the notification model in the graphql endpoint when I query the notification.这工作正常,但是当我查询通知时,我想要由 HistoricalRecords() 为 graphql 端点中的通知 model 创建的所有记录。 How can I do this?我怎样才能做到这一点?

I assume you want each notification's history inside it, probably to show it somewhere in your app.我假设您希望其中包含每个通知的历史记录,可能会在您的应用程序的某个位置显示它。

Getting a change list from a list of revisions从修订列表中获取更改列表

Looks like there's an issue you'll have to solve first - django-simple-history stores revisions of objects, and not the diffs.看起来您必须首先解决一个问题 - django-simple-history 存储对象的修订版,而不是差异。 You'll have figure out a way to convert this你会想出一种方法来转换它

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

to something like this by comparing adjacent revisions and finding the differences:通过比较相邻的修订版并找到差异来达到类似的效果:

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

In reality you probably need dicts instead of stings, so it's easier to process on frontend.实际上,您可能需要 dicts 而不是 stings,因此在前端处理起来更容易。

There is a tool included in django-simple-history just for that. django-simple-history 中包含一个工具 Here's some pseudocode of how you might do it:下面是一些伪代码,你可以如何做到这一点:

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

Adding the changelist to graphql将更改列表添加到 graphql

We'll have to declare a separate ObjectType for changelist objects:我们必须为更改列表对象声明一个单独的 ObjectType:

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

After that, we declare a custom field with a resolver on NotificationNode :之后,我们在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()

Keep in mind that this was not tested, it's just pseudocode and definitely contains bugs.请记住,这没有经过测试,它只是伪代码并且肯定包含错误。 This should provide a starting point though.不过,这应该提供一个起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM