引用Django中的模型实例的“机器标签” [英] ‘Machine tags’ referencing model instances in Django

查看:145
本文介绍了引用Django中的模型实例的“机器标签”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,您有型号出版物照片事件 Person ;他们可以以不同的方式相互联系。特别是出版物可以有1)一些插图(相关的照片)和2)一些提到的角色。活动可以有一些3)照片和4)人们相关。此外,5)事件可以在出版物中提及。

Say, you have models Publication, Photo, Event and Person; they could relate to each other in different ways. Particularly, publications can have 1) some illustrations (related photos) and 2) some mentioned personas. Events can have some 3) photos and 4) people related as well. Also, 5) events could be mentioned in publications.

除了插图,没有其他数据需要与关系相关联:他们需要一个位置字段进行排序。所以在这种情况下(#1),可以使用中间模型,如插图引用照片和发布,并指定一个位置字段。

No additional data needs to be associated with relationships, except for illustrations: they need a position field for sorting. So in that case (#1), it's OK to go with intermediate model like Illustration referencing photo and publication and specifying a position field.

但是情况2或4变得复杂。

But with cases 2 or 4 it becomes complicated.

说明一个 ReferencedPerson 它可以指向任何类型的对象(通过通用外键),因此它可以用于发布和事件。另一方面,您可以指定 ReferencedEvent ,这可以在情况5和3中使用,但也可以在情况4中使用,因为您不能真正地说这是一个引用一个人而不是反之亦然的事件。

It makes sense to specify, say, a ReferencedPerson which can point to object of any type (via generic foreign key), so it could be used for both publications and events. On the other hand, you can specify ReferencedEvent, which could be used in cases 5 and 3, but also in the case 4 as well, because you can't really say that it's an event which references a person and not vice-versa.

所以有一点,它开始考虑只是指定一个 ReferencedObject具有两个通用外键的模型。这是不真实的,这是标签是什么,如果标签可以代表另一个模型实例,那将是巨大的。

So at one point it becomes unDRY enough to start thinking about simply specifying a ReferencedObject model with two generic foreign keys. And this is uncool, this is what tags are for, and if tags could represent another model instances, that would be great.

机器标签是可以包含命名空间和/或值的标签。它们看起来像命名空间:tag = value (可以使用命名空间和值部分)。

Machine tags are tags which could contain namespaces and/or values. They look like namespace:tag=value (with namespace and value parts optional).

可以使用机器标签不仅要保存额外的数据,而要表示对象之间的关系。粗略的例子将是Flickr,您可以在其中标记照片,例如即将到来:event = 81334 ,它将自动显示为即将到来的链接。

Machine tags can be used not only to hold extra data, but to represent relationships between objects. Rough example would be Flickr, where you can tag a photo with, say, upcoming:event=81334 and it will be automatically displayed as a link to Upcoming.

machinetags $ c> django-tagging 应用程序。这是非常通用的,标签不被认为是引用对象。我想要写一个提供帮助函数的应用程序和一个模板标签或过滤器,它将替换特殊机器标签,例如使用引用对象的人类可读名称。

There's machinetags branch of django-tagging application. It's quite generic and tags there are not considered as referencing objects. I think of writing a app providing helper functions and a template tag or filter which would replace "special" machine tag, say, with referenced object's human readable name.

(抱歉多于一个。)使用机器标签建立模型之间建立关系是否有意义?如果它用Django来实现的最好方法是什么?最好使用中间模型的情况是什么?我在这里解决了错误的问题吗?

(Sorry for more than one.) Does it make sense to build relationships between models like that, with machine tags? And if it does, what would be the best way to implement that with Django? What are the cases when it's better to use intermediate models? Am I solving the wrong problem here?

推荐答案

我不认为使用标记是适合代表关系的。当然,这一切都取决于你想要执行什么样的查询。对于常规的导航查询,应使用常规的n:m关系。当信息结构现在已知时,应使用标记。

I don't think the use of tagging is appropriate for representing relationships. Of course, it all depends on what kind of queries you want to perform. For regular navigation queries, regular n:m relations should be used. Tagging should be used when the information structure is now known upfront.

这篇关于引用Django中的模型实例的“机器标签”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆