Note that there are some explanatory texts on larger screens.

plurals
  1. PO‘Machine tags’ referencing model instances in Django
    text
    copied!<h3>Example problem</h3> <p>Say, you have models <code>Publication</code>, <code>Photo</code>, <code>Event</code> and <code>Person</code>; 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.</p> <p>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 <code>Illustration</code> referencing photo and publication and specifying a position field.</p> <p>But with cases 2 or 4 it becomes complicated.</p> <p>It makes sense to specify, say, a <code>ReferencedPerson</code> 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 <code>ReferencedEvent</code>, 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.</p> <p>So at one point it becomes unDRY enough to start thinking about simply specifying a <code>ReferencedObject</code> 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.</p> <h3>The idea</h3> <p><em>Machine tags</em> are tags which could contain namespaces and/or values. They look like <code>namespace:tag=value</code> (with namespace and value parts optional).</p> <p>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, <code>upcoming:event=81334</code> and it will be automatically displayed as a link to Upcoming.</p> <h3>Possible variants of implementation</h3> <p>There's <code>machinetags</code> branch of <code>django-tagging</code> 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.</p> <h3>The question</h3> <p>(Sorry for more than one.) <strong>Does it make sense to build relationships between models like that, with machine tags?</strong> 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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload