Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginner: Trying to understand how apps interact in Django
    primarykey
    data
    text
    <p>I just got done working through the Django tutorials for the second time, and am understanding things much more clearly now. However, I'm still unclear how apps inside a site interact with one another.</p> <p>For example, lets say I'm writing a blog application (a rather popular activity, apparently). Blog posts and comments tend to go together, and yet they are distinct enough that they should be built into separate apps, as is the general philosophy of Djano development.</p> <p>Consider the following example. In reality I would not actually write the comment app myself, as good code for that already exists on the web, but this is for demonstration/practice purposes:</p> <p><strong>mysite/blog/models.py</strong></p> <pre><code>from django.db import models class post(models.Model): title = models.CharField(max_length=200) author = models.CharField(max_length=200) content = models.TextField() </code></pre> <p><strong>mysite/comments/models.py</strong></p> <pre><code>from django.db import models from mysite.blog.models import post class comment(models.Model): id = models.AutoField() post = models.ForeignKey(post) author = models.CharField(max_length=200) text = models.TextField() </code></pre> <p>Is what I wrote above, importing a model from another app and setting it as a foreign key, how Django apps interact? Or is there a different/better method for the apps that comprise a site to interact?</p> <p><strong>Update</strong><br> Per the recommendation in one response, I'm reading the documentation for contrib.contenttypes. If I'm reading this correctly, I could rewrite my example comment app like this: </p> <pre><code>from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.contentypes import generic class comment(models.Model): id = models.AutoField() author = models.CharField(max_length=200) text = models.TextField() content_type = models.ForeignKey(ContentType) content_object = generic.GenericForeignKey(content_type, id) </code></pre> <p>Would this be correct?</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