Python django模型继承&转换为视图中的子类型
这是一种cms类型的应用程序 我有一个文章模型和models.py的一些专门知识Python django模型继承&转换为视图中的子类型,python,django,Python,Django,这是一种cms类型的应用程序 我有一个文章模型和models.py的一些专门知识 class Article(models.Model): foo = models.CharField(max_length=50) bar = models.CharField(max_length=100,blank=True) DISPLAY_CHOICES = ( ('N', 'None'), ('C','Carousel'), ('M
class Article(models.Model):
foo = models.CharField(max_length=50)
bar = models.CharField(max_length=100,blank=True)
DISPLAY_CHOICES = (
('N', 'None'),
('C','Carousel'),
('M','Marketing'),
('F','Featurette')
)
display = models.CharField(max_length=1, choices = DISPLAY_CHOICES)
def __unicode__(self):
return self.title
class Artist(Article):
website = models.URLField(max_length=200,blank=True)
class Venue(Article):
location = models.CharField(max_length=150)
map_link = models.URLField(max_length=200,blank=True)
class Event(Article):
time = models.DateTimeField()
venue = models.ForeignKey(Venue)
performers = models.ManyToManyField(Artist)
我想根据article.display的值以不同的方式呈现这些内容,但在调用
articles.objects.all()
我仍然需要子类的额外属性,所以我写了
#views.py
def castToSubClass(article):
try:
return Artist.objects.get(article_ptr_id = article.id)
except:
try:
return Event.objects.get(article_ptr_id = article.id)
except:
try:
return Venue.objects.get(article_ptr_id = article.id)
except:
return article
def index(request):
carousel = [castToSubClass(article) for article in Article.objects.filter(display='C']
marketing = [castToSubClass(article) for article in Article.objects.filter(display='M'[:3]]
featurettes = [castToSubClass(article) for article in Article.objects.filter(display='F']
return render_to_response('frontpage.html',
{
'carousel': carousel,
'marketing':marketing,
'featurettes': featurettes
})
为了将它们都转换为适当的子类对象,除了看起来笨重之外,这似乎意味着我对queryset中的每一项或几乎每一项都要访问数据库两次
有没有办法在最初给经理打电话时做到这一点
谢谢。使用一个模型来存储所有内容,并添加一个字段来区分文章类型,这样您就可以为每种类型呈现不同的外观,并在类似模板的tumblr do中显示
class Article(models.Model):
foo = models.CharField(max_length=50)
bar = models.CharField(max_length=100,blank=True)
DISPLAY_CHOICES = (
('N', 'None'),
('C','Carousel'),
('M','Marketing'),
('F','Featurette')
)
display = models.CharField(max_length=1, choices = DISPLAY_CHOICES)
ARTICLE_TYPE_CHOICES = (
('artist', 'Artist'),
('venue', 'Venue'),
('event', 'Event'),
)
type = models.CharField(max_length=32, choices = ARTICLE_TYPE_CHOICES)
website = models.URLField(max_length=200,blank=True, null=True)
location = models.CharField(max_length=150, blank=True, null=True)
map_link = models.URLField(max_length=200,blank=True, null=True)
time = models.DateTimeField(null=True)
venue = models.ForeignKey('self', null=True)
performers = models.ManyToManyField('self', null=True)
def __unicode__(self):
return self.title
#views.py
def index(request):
carousel = Article.objects.filter(display='C')
marketing = Article.objects.filter(display='M')
featurettes = Article.objects.filter(display='F')
return render_to_response('frontpage.html',{'carousel': carousel, 'marketing':marketing, 'featurettes': featurettes})
谢谢,麻烦是。。。然后,我必须在模型中有一堆blank=True字段,必须有一种方法可以做到这一点,而不需要组合现有的类。如果不是这样,那么Django中的子类化模型的整个想法都应该被摒弃。这是关于针对特定需求的数据库结构设计。Django模型继承有其特定的情况。