Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to model one way one-to-one relationship in Django
    primarykey
    data
    text
    <p>I want to model an article with revisions in Django:</p> <p>I have following in my article's models.py:</p> <pre><code>class Article(models.Model): title = models.CharField(blank=False, max_length=80) slug = models.SlugField(max_length=80) def __unicode__(self): return self.title class ArticleRevision(models.Model): article = models.ForeignKey(Article) revision_nr = models.PositiveSmallIntegerField(blank=True, null=True) body = models.TextField(blank=False) </code></pre> <p>On the artlcle model I want to have 2 direct references to a revision - one would point to a published revision and another to a revision that is being actively edited. However from what I understand, OneToOne and ForeignKey references generate a backreference on the other side of the model reference, so my question is, how do i create a one-way one-to-one reference in Django?</p> <p>Is there some special incantation for that or do I have to fake it by including state into revision and custom implementations of the fields that ask for a revision in specific state?</p> <p><strong>Edit:</strong> I guess, I've done somewhat poor job of explaining my intent. Let's try it on a higher abstraction level:</p> <p>My original intent was to implement a sort of revisioned article model, where each article may have multiple revisions, where one of those revisions may be "published" and one actively edited.</p> <p>This means that the article will have one-to-many relationship to revisions (represented by <code>ForeignKey(Article)</code> reference in <em>ArticleRevision</em> class) and two one way references from Article to revision: <em><code>published_revision</code></em> and <em><code>edited_revision</code></em>.</p> <p>My question is mainly, how can I model this with Django's ORM.</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.
 

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