Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at django's built-in <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#ref-contrib-contenttypes" rel="noreferrer">contenttypes framework</a>:</p> <p><code>django.contrib.contenttypes</code></p> <p>It allows you develop your applications as stand-alone units. This is what the django developers used to allow django's built-in <a href="http://docs.djangoproject.com/en/dev/ref/contrib/comments/#ref-contrib-comments-index" rel="noreferrer">comment framework</a> to attach a comment to any model in your project.</p> <p>For instance, if you have some content object that you want to "attach" to other content objects of different types, like allowing each user to leave a "favorite" star on a blog post, image, or user profile, you can create a <code>Favorite</code> model with a <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations" rel="noreferrer">generic relation field</a> like so:</p> <pre><code>from django.db import models from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic class Favorite(models.Model): user = models.ForeignKey(User) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') </code></pre> <p>In this way you can add a <code>Favorite</code> star from any user to any model in your project. If you want to add API access via the recipient model class you can either add a <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#reverse-generic-relations" rel="noreferrer">reverse generic relation field</a> on the recipient model (although this would be "coupling" the two models, which you said you wanted to avoid), or do the lookup through the <code>Favorite</code> model with the <code>content_type</code> and <code>object_id</code> of the recipient instance, see the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#reverse-generic-relations" rel="noreferrer">official docs</a> for an example. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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