Django模型继承,过滤模型 [英] Django model inheritance, filtering models

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

问题描述

给出以下模型:(不要介意TextFields只是为了说明)

  class Base(models .Model):
field1 = models.TextField()

class Meta:
abstract = True

class Child1(Base):
child1_field = models.TextField()

class Child2(Base):
child2_field = models.TextField()


类内容(模型。模型):
aso_items = models.ManyToManyField('Base')

根据这些定义一个Content对象可以与多个Base对象相关联,例如。面试(=内容对象)可以与音乐家(= Child1对象),电影导演(= Child2)等联系。



现在,对于我的问题:
是否可以根据aso_items字段指向哪个模型来过滤Content对象?
一个例子:说我想要一个包含与Child1的特定对象相关联的所有Content对象的Queryset(例如,与音乐人Bob Dylan相关的所有访问),我该如何实现?此外,如果我想要一个包含与Child1对象相关联的所有Content对象的QuerySet(例如与音乐人相关联的所有访问),该怎么办?
如何更改过滤?



提前感谢
ps:我在预览中遇到一些空白的问题,原谅我


解决方案

您应该查看Django文档中有关使用 related_name 的部分摘要基础课程 http://docs.djangoproject。 com / en / dev / topics / db / models /#be-careful-with-related-name



引用文档:


如果您在ForeignKey或
上使用 related_name
属性ManyToManyField,您必须始终

字段指定唯一的反向名称。这通常会导致抽象基类
中的
问题,因为这个类的字段是
包含在每个子
类中,具有完全相同的值
的属性(包括
related_name )每次。



要解决此问题,你
在抽象的
基类(仅)中使用related_name,名称
的一部分应该是字符串%(class)s

的子类替换为
的子类名称,因为每个类都有一个不同的
名称,每个相关名称将会结束
不同。


使用这些信息我建议将m2m字段移动到Base类中:

 类内容(models.Model):
#添加内容
pass的剩余字段

class Base(models.Model):
field1 = models.TextField()
items = models.ManyToManyField(Content,related_name =%(class)s_related)

class Meta:
abstract = True

class Child1(Base):
child1_field = models.TextField()

class Child2(Base):
child2_field = models.TextField()


Given the following models:(don't mind the TextFields there're just for illustration)

class Base(models.Model):
   field1 = models.TextField()

   class Meta:
      abstract=True

class Child1(Base):
   child1_field = models.TextField()

class Child2(Base):
   child2_field = models.TextField()


class Content(models.Model):
    aso_items = models.ManyToManyField('Base')

According to these definitions a Content object can be associated with more than one Base object, eg. an interview(=Content object) can be linked with a musician(=Child1 object), a filmdirector(=Child2), etc.

Now, for my question: Is it possible to filter Content objects according to which model the aso_items field points to? An example : Say I would like a Queryset containing all the Content objects that are associated with a specific object of Child1(eg. all the interviews associated with the musician Bob Dylan), how can I achieve this?

Further, what if I'd want a QuerySet containing all the Content objects that are associated with Child1 objects?(eg. all the interviews that associated with musicians) How does this change the filtering?

Thanks in advance ps: I'm experiencing some problems with white space in the preview, forgive me

解决方案

You should check the section of the Django docs regarding using related_name for abstract base classes. http://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name

To quote the docs:

If you are using the related_name attribute on a ForeignKey or ManyToManyField, you must always specify a unique reverse name for the field. This would normally cause a problem in abstract base classes, since the fields on this class are included into each of the child classes, with exactly the same values for the attributes (including related_name) each time.

To work around this problem, when you are using related_name in an abstract base class (only), part of the name should be the string %(class)s. This is replaced by the lower-cased name of the child class that the field is used in. Since each class has a different name, each related name will end up being different.

Using this information I would recommend moving the m2m field into the Base class:

class Content(models.Model):
   # Add remaining fields for Content 
   pass

class Base(models.Model):
   field1 = models.TextField()
   items = models.ManyToManyField(Content,related_name="%(class)s_related")

   class Meta:
      abstract=True

class Child1(Base):
   child1_field = models.TextField()

class Child2(Base):
   child2_field = models.TextField()

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

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