Django模型继承查询中心表 [英] Django Model Inheritance query a central table

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

问题描述

我有一个解决方案,我以为我可以照顾模型继承,但现在再看一遍它并不能解决我的问题。我想要的是能够调用一个模型,然后让我的孩子模型的领域可以访问。继承我还要把命名行中的小孩型号命名为全部目的。以下是我想要的例子:

  class LessonModule(models.Model):
lesson = models。 ForeignKey('Lesson')
name = models.CharField(max_length = 100,blank = True)


class ProfileImage(LessonModule):
file_loc = models。 ImageField(upload_to =profiles /)
detail_file_loc = models.ImageField(upload_to =profiles /,blank = True)

def render(self):
t = loader .get_template(template / profile_image.html)
c = Context({'image_path':self.file_loc.url})
return t.render(c)

def __unicode __(self):
return'[prof:%d]'%self.id

class注意(LessonModule):
def __unicode __(self):
返回'[note:%d]'%self.id

def render(self):
return self.id

我想要做的是做一个:

  module = LessonModule.objects.g et(pk = 20)
module.render()

并运行相应的渲染功能。例如,如果pk与Note模型对齐,那么它将返回self.id。当然,这简化了我对这些功能的想法。



我不必使用模型继承。它只是看起来像这样做的最好方式。我只想要一个中心区域来查找所有可能的模块。



我还将使用它来从课程中的外键中分配给课程的所有LessonModule LessonModule。

解决方案

我没有使用它,但这个项目看起来像你要找的:

https://code.google.com/p/django-polymorphic-models /



您可以将每个对象的LessonModule.objects.all()和.downcast()自动请求到ProfileImage或Note中。



或将PolymorphicMetaclass添加到您的LessonModule中,以始终从查询集中检索ProfileImage和Note对象。



注意额外查询的费用... Django模型中的多态性通过表连接完成,而不是纯Python代码。


I have a solution that I thought I could take care of model inheritance, but now looking at it again it doesn't actually solve my problem. What I would like is to be able to call one model and then have the fields of the children model accessible to me. With inheritance I still have to put the child model name in the command line which defeats the whole purpose. Here is an example of what I would like:

class LessonModule(models.Model):
    lesson = models.ForeignKey('Lesson')
    name = models.CharField(max_length=100, blank=True)


class ProfileImage(LessonModule):
    file_loc = models.ImageField(upload_to="profiles/")
    detail_file_loc = models.ImageField(upload_to="profiles/", blank=True)

    def render(self):
        t = loader.get_template("template/profile_image.html")
        c = Context({'image_path': self.file_loc.url})
        return t.render(c)

    def __unicode__(self):
        return '[prof: %d]' % self.id

class Note(LessonModule):
    def __unicode__(self):
        return '[note: %d]' % self.id

    def render(self):
        return self.id

What I would like to be able to do is do a:

module = LessonModule.objects.get(pk=20)
module.render()

And have it run the corresponding render function. For example if the pk aligns with the Note model then it would just return the self.id. Of course this is simplified to what I want to do with these functions.

I don't have to use Model Inheritance. It just looked like the best way of doing this. I just want a central area to look for all the possible modules.

I would also use this to pull all the LessonModules assigned to a Lesson from the lesson foreign key in the LessonModule.

解决方案

I haven't used it, but this project looks like what you're looking for:
https://code.google.com/p/django-polymorphic-models/

You can request LessonModule.objects.all() and then .downcast() each object automatically into a ProfileImage or a Note.

Or add the PolymorphicMetaclass to your LessonModule to always retrieve ProfileImage and Note objects from a queryset.

Note the cost of extra queries... polymorphism in Django models is done via table joins, not pure python code.

这篇关于Django模型继承查询中心表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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