Django继承和固定链接 [英] Django Inheritance and Permalinks

查看:216
本文介绍了Django继承和固定链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中创建一个简单的CMS,有多个模块(每个都是django应用程序)。我设置了以下模型:

  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')

在每个模块中,我创建一个FooPage的子类,至少有一个子类的FooSubitem,例如

 #在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)

  #在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')

这些都是简化,但应该给你一个很好的想法,我正在尝试去做。在FooPost和FooImage的管理员中,我将父选择列表限制到其相应的父页面。



当我尝试在模板中使用这些列表时,我的问题出现。在每个视图中,我有以下内容:

  page_list = FooPage.objects.all()

它返回FooBlog和FooGallery类型的所有FooPages列表。但是,当我重复列表时:

  {%page_list%}中的页面{{page.get_absolute_url}} {% endfor%} 

它返回页面的网址格式,而不是博客或图库 url模式。



稍后我要添加一个FooCalendar模块时,如何使这项工作无需重写代码呢?我想确保这适用于任何可能的模块。



谢谢,




  • Lexo


解决方案

这个问题的经典解决方案往往是将ContentType添加到存储该实例的子类型的超类。这样,您可以依赖一个一致的API,返回适当类型的相关子类对象。


I'm creating a simple CMS in django, with multiple "modules" (each as a django app). I've set up the following models:

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')

In each of the modules, I create a subclass of FooPage, and at least one subclass of FooSubitem, e.g.

# 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)

and

# 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')

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.

My problem arises when I try to use these in a template. In each view, I have the following:

page_list = FooPage.objects.all()

which returns a list of all FooPages, of both FooBlog and FooGallery types. However, when I iterate through this list:

{% for page in page_list %}{{ page.get_absolute_url }}{% endfor %}

it returns the 'page' url pattern, not the 'blog' or 'gallery' url pattern.

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.

Thanks,

  • Lexo

解决方案

The classic solution to this problem tends to be adding a ContentType to the superclass which stores the type of subclass for that instance. This way you can rely on a consistent API that returns the related subclass object of the appropriate type.

这篇关于Django继承和固定链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆