Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何访问”的方法;“子模型”;在Django?_Python_Django_Django Models - Fatal编程技术网

Python “如何访问”的方法;“子模型”;在Django?

Python “如何访问”的方法;“子模型”;在Django?,python,django,django-models,Python,Django,Django Models,在Django中,我有以下模型.py class Product(RandomPrimaryIdModel): feature1 = models.CharField(max_length=20, blank=True, null=True) feature2 = models.CharField(max_length=20, blank=True, null=True) feature3 = models.CharField(max_length=20, blank=True, n

在Django中,我有以下模型.py

class Product(RandomPrimaryIdModel):
  feature1 = models.CharField(max_length=20, blank=True, null=True)
  feature2 = models.CharField(max_length=20, blank=True, null=True)
  feature3 = models.CharField(max_length=20, blank=True, null=True)

class Mattress(Product):
  category_type = models.CharField(max_length=50)
  size = models.CharField(max_length=5)

  def category(self):
    return "bedding"
  category = property(category)
我有以下views.py文件

def update(request, id):
  product = Product.objects.get(id=id)
  ...

在这个方法update中,我可以从产品模型调用“床垫”模型中定义的方法。例如,我想写:if product.type==“床垫”,其中类型已在床垫模型中定义,床垫是产品的子模型。

您的示例似乎介于两种不同的方式之间,但目前不正确。所发生的事情是,您正在创建两个表:产品和床垫,它们完全不相关。不管床垫是产品的子类,它只是继承了它的结构。您不能在产品表中查询任何有关床垫的信息,因为床垫在床垫表中

<> P>一种方法是考虑一个抽象的产品,要被实际产品细分:

class Product(RandomPrimaryIdModel):
    class Meta:
        abstract=True
这将阻止创建产品表。然后通过:
matd.objects.filter()直接查询床垫

但这似乎有点限制了引入多种类型的产品,并且必须为它们管理不同的表。另一种方法是使用产品表,但使用泛型关系来支持将任何类型的其他表附加为内容对象:

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Product(RandomPrimaryIdModel):

    feature1 = models.CharField(max_length=20, blank=True, null=True)
    feature2 = models.CharField(max_length=20, blank=True, null=True)
    feature3 = models.CharField(max_length=20, blank=True, null=True)

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
这样,您就可以将
content\u对象
设置为床垫实例。然后,您可以使用ContentType查询:

p_type = ContentType.objects.get(name="mattress")
Product.objects.filter(content_type=p_type)

这看起来像是一个自动下浇铸的案例。我需要一种类似的方法来处理一个购物车,该购物车包含通用的“ProductBase”实例,但我需要访问子类的特定函数,这些函数是ProductDownloadable、ProductShipped等类型的实际产品

Django本机不支持这一点,但可以通过内省或使用对其进行编码,一旦安装,您可以执行以下操作:

# return a list 'child' classes of Product - in your case Mattresses
mattress_list = Product.objects.all().select_subclasses() 

# return the direct 'child' class of Product - in your case Mattress class
mattress = Product.get_subclass(id=some_id) # returns the 'child' subclass
mattress.foo() # executes method on foo on Mattress class (not on Product class)

您只需获取一个床垫实例并将其指定给content\u对象。模型将为您填充其他字段。content_对象实际上不是数据库中的字段。它是为您解析类型和对象id的帮助器属性。是的,您可能需要为其他类型创建更多模型。或者,您需要考虑一种创建ProductDetails对象的方法,该对象可以存储所有类型产品的所有数据。必须有某种类型的表架构。因此,您可以针对每种产品类型创建一个表,也可以创建一个通用的单表。快速跟进:产品模型将所有类型的产品(例如:标题、条件、价格)共有的字段分解出来。对于内容类型解决方案,如何维护这些分解字段,或者我需要为每个产品类型指定它们?使用单个表可能是更易于管理的方法,因为这意味着您可以轻松地动态添加产品,而无需创建新表。但这需要一个包含一组通用字段的产品表,然后是另一个将通用字段映射到该产品类型的命名字段的表。然后,您需要有一个设置,为每个产品创建代理对象,向您显示属性,但在后台将它们保存回通用映射对象。老实说,我还从来没有做过基于产品的django网站,如果我做了,我可能只会使用django现有的购物应用程序框架来回答您的后续问题,product表仍然可以为每个产品提供所有这些公共字段,并且应该这样做。特定产品字段仅用于特定于产品的元信息