Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Besides the admin page inlines, other reason for the <code>ForeignKey</code> solution is that it allows you to use the correct, default DB manager when objects are accessed with a reverse relation. Consider example from this <a href="http://djangosnippets.org/snippets/1037/" rel="noreferrer">subclasses manager snippet</a>. Let's say that the <code>Post</code> class definition from the example looks like this:</p> <pre><code>class Post(ParentModel): title = models.CharField(max_length=50) onetoone = models.ForeignKey(SomeModel, unique=True) children = ChildManager() objects = models.Manager() </code></pre> <p>By calling <code>somemodel_instance.post_set.all()[0]</code>, you get the desired subclasses objects of the <code>Post</code> class as indicated by defining the first (default) manager as a <code>ChildManager</code>. On the other hand, with <code>OneToOneField</code>, by calling <code>somemodel_instance.post</code> you get the <code>Post</code> class instance. You can always call <code>somemodel_instance.post.subclass_object</code> and get the same result, but the default manager could do any other sort of tricks and the <code>FK</code> solutions hides them nicely.</p> <p>If you own and can modify the custom manager code you can use the <a href="http://docs.djangoproject.com/en/dev/topics/db/managers/#manager-types" rel="noreferrer"><code>use_for_related_fields</code></a> attribute instead of using FK in place of legitimate 1to1 field, but even that can fail because of some not-known to me nuisances of the automatic managers. As far as I remember it will fail in the above 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. 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.
    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