Django:如何让用户定义对象之间的语义关系?

发布于 2024-11-19 06:50:08 字数 313 浏览 1 评论 0原文

在 mediawiki 中,用户只需输入 [[页面名称]] 即可链接到页面。在语义媒体维基中,用户可以通过将 [[example::Some Page]] 标识为示例页面来与页面建立语义关系。

我希望能够使用 django 模型来做到这一点。

例如,我希望用户能够在工作流应用程序中编写任务对象的描述,并添加类似“跟进 [[User.id:43]]”的内容。

我也希望能够做语义方面的事情。

最后,我希望能够查看用户 43,看看有哪些模型链接到它。

有没有 django 包可以完成部分或全部工作?如果不是,这种方法通常被称为什么?

In mediawiki, a user can link to a page by just entering the [[Name of the Page]]. In semantic mediawiki, a user can make a semantic relationship to a page by identifying [[example::Some Page]] as an example page.

I want to be able to do this with django models.

For example, I want a user to be able to write a description for a Task object in a workflow app, and put something like "Follow up with [[User.id:43]]."

I'd also love to be able to do the semantic thing.

Finally, I'd like to be able to look at User 43 and see what models link to it.

Is there a django package that will do some or all of this? If not, what is this methodology called generally?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-11-26 06:50:08

首先,我设置以下内容来处理链接检测和创建:

  1. 创建一个 ModelForm 来接收用户提交的 Task。创建一个自定义验证器来操作描述字段。该验证器将检查用户输入的任何链接是否符合您指定的 [[:]] 格式。
  2. 覆盖保存方法 任务模型。这是检查描述内容并根据需要创建链接的好时机。

现在,在 save 方法中,真正的动作正在发生。我会使用正则表达式来提取所有链接,然后一一处理它们。

  1. 对于每个链接,您需要确定链接到的模型。通过键入模型名称的模型字典,这将很容易。
  2. 您需要确定该链接是否确实标识了有效的模型实例。如果没有,请跳过此链接或引发异常并退出整个流程(请参阅上文有关交易的内容)。
  3. 您需要创建链接。见下文。

Django 有一个标准的通用外键机制,你应该使用它绝对考虑在这里使用。您可以创建一个链接类,如下所示:

class Link(models.Model):
    # link to particular task:
    task = models.ForeignKey(Task)

    # these three fields together set up a generic foreign key which 
    # represents the object you're linking to:
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

然后,您的任务模型对象将自动获取一个名为 link_set 的属性,该属性将是 Link 实例的列表。

创建链接看起来像这样:

# encountered a description with [[User:43]]
instance = User.objects.get(pk=43)
link = Link.objects.create(task=my_task_object, content_object=instance)

通过为链接的 content_object 属性提供另一个模型的实例,其 content_typeobject_id 字段将自动填充您,您的链接将解析回指定的实例。

希望这有帮助。如果您需要,请询问更多详细信息。

First of all, I'd set up the following to handle link detection and creation:

  1. Create a ModelForm to receive the user's submitted Task. Create a custom validator to operate on the description field of this form. This validator would check that any links entered by the user correspond to the the [[:]] format you've specified.
  2. Override the save method on the Task model. This is a good time to inspect the contents of the description and create links as appropriate.

Now, in the save method, the real action is happening. I would use a regex to pull out all the links and then work through them one by one.

  1. For each link, you need to determine the model linked to. This would be easy with a dictionary of models keyed on their names.
  2. You need to determine if the link has actually identified a valid model instance. If not, either skip this link or raise an exception and eject from the whole process (see above regarding transactions).
  3. You need to create the links. See below.

Django has a standard mechanism for generic foreign keys which you should definitely consider using here. You could create a link class something like:

class Link(models.Model):
    # link to particular task:
    task = models.ForeignKey(Task)

    # these three fields together set up a generic foreign key which 
    # represents the object you're linking to:
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

Your Task model objects would then automatically get an attribute called link_set which would be a list of Link instances.

Creating a link would look something like:

# encountered a description with [[User:43]]
instance = User.objects.get(pk=43)
link = Link.objects.create(task=my_task_object, content_object=instance)

By giving Link's content_object attribute an instance of another model, its content_type and object_id fields are automatically filled in for you, and your link will resolve back to that specified instance.

Hope this is helpful. Ask for more detail if you need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文