Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with understanding generic relations in Django (and usage in Admin)
    primarykey
    data
    text
    <p>I'm building a CMS for my company's website (I've looked at the existing Django solutions and want something that's much slimmer/simpler, and that handles our situation specifically.. Plus, I'd like to learn this stuff better). I'm having trouble wrapping my head around generic relations.</p> <p>I have a <code>Page</code> model, a <code>SoftwareModule</code> model, and some other models that define content on our website, each with their <code>get_absolute_url()</code> defined. I'd like for my users to be able to assign any <code>Page</code> instance a list of objects, of any type, including other page instances. This list will become that <code>Page</code> instance's sub-menu.</p> <p>I've tried the following:</p> <pre><code>class Page(models.Model): body = models.TextField() links = generic.GenericRelation("LinkedItem") @models.permalink def get_absolute_url(self): # returns the right URL class LinkedItem(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') title = models.CharField(max_length=100) def __unicode__(self): return self.title class SoftwareModule(models.Model): name = models.CharField(max_length=100) description = models.TextField() def __unicode__(self): return self.name @models.permalink def get_absolute_url(self): # returns the right URL </code></pre> <p>This gets me a generic relation with an API to do <code>page_instance.links.all()</code>. We're on our way. What I'm not sure how to pull off, is on the page instance's change form, how to create the relationship between that page, and any other extant object in the database. My desired end result: to render the following in a template:</p> <pre><code>&lt;ul&gt; {% for link in page.links.all %} &lt;li&gt;&lt;a href='{{ link.content_object.get_absolute_url() }}'&gt;{{ link.title }}&lt;/a&gt;&lt;/li&gt; {% endfor%} &lt;/ul&gt; </code></pre> <p>Obviously, there's something I'm unaware of or mis-understanding, but I feel like I'm, treading into that area where I don't know what I don't know. What am I missing?</p>
    singulars
    1. This table or related slice is empty.
    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. 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