Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango Inheritance and Permalinks
    text
    copied!<p>I'm creating a simple CMS in django, with multiple "modules" (each as a django app). I've set up the following models:</p> <pre><code>class FooObject(models.Model): id = models.SlugField(primary_key=True) name = models.CharField(max_length=255) creator = models.ForeignKey(auth.models.User, editable=False, related_name="createdby") class FooPage(FooObject): content = models.TextField(blank=True, null=True) @models.permalink def get_absolute_url(self): return ('page', (), {'page_id':self.id} class FooSubitem(FooObject): parent = models.ForeignKey(FooPage, related_name='subitems') </code></pre> <p>In each of the modules, I create a subclass of FooPage, and at least one subclass of FooSubitem, e.g.</p> <pre><code># in FooBlog.models class FooBlog(FooPage): owner = models.ForeignKey(auth.models.User, editable=False) @models.permalink def get_absolute_url(self): return ('blog', (), {'blog_id':self.id}) class FooPost(FooSubitem): post_time = models.DateTimeField(auto_now_add=True) </code></pre> <p>and</p> <pre><code># in FooGallery.models class FooGallery(FooPage): location = models.CharField(max_length=255) @models.permalink def get_absolute_url(self): return ('gallery', (), {'gallery_id':self.id}) class FooImage(FooSubitem): image_file = models.ImageField(upload_to='foogallery') </code></pre> <p>These are simplifications, but should give you a good idea of what I'm trying to do. In the admins for FooPost and FooImage, I restrict the parent selection list to their corresponding parent pages.</p> <p>My problem arises when I try to use these in a template. In each view, I have the following:</p> <pre><code>page_list = FooPage.objects.all() </code></pre> <p>which returns a list of all FooPages, of both FooBlog and FooGallery types. However, when I iterate through this list:</p> <pre><code>{% for page in page_list %}{{ page.get_absolute_url }}{% endfor %} </code></pre> <p>it returns the 'page' url pattern, not the 'blog' or 'gallery' url pattern.</p> <p>How do I make this work without having to rewrite the code when I want to add a FooCalendar module later on? I want to make sure this works with any possible module.</p> <p>Thanks,</p> <ul> <li>Lexo</li> </ul>
 

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