Django:如何让用户定义对象之间的语义关系?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我设置以下内容来处理链接检测和创建:
Task
。创建一个自定义验证器来操作描述字段。该验证器将检查用户输入的任何链接是否符合您指定的 [[:]] 格式。
任务
模型。这是检查描述内容并根据需要创建链接的好时机。现在,在 save 方法中,真正的动作正在发生。我会使用正则表达式来提取所有链接,然后一一处理它们。
Django 有一个标准的通用外键机制,你应该使用它绝对考虑在这里使用。您可以创建一个链接类,如下所示:
然后,您的任务模型对象将自动获取一个名为
link_set
的属性,该属性将是 Link 实例的列表。创建链接看起来像这样:
通过为链接的
content_object
属性提供另一个模型的实例,其content_type
和object_id
字段将自动填充您,您的链接将解析回指定的实例。希望这有帮助。如果您需要,请询问更多详细信息。
First of all, I'd set up the following to handle link detection and creation:
Task
. Create a custom validator to operate on thedescription
field of this form. This validator would check that any links entered by the user correspond to the the [[:]] format you've specified.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.
Django has a standard mechanism for generic foreign keys which you should definitely consider using here. You could create a link class something like:
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:
By giving Link's
content_object
attribute an instance of another model, itscontent_type
andobject_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.