【问题标题】:How do I make a class a sub-group of another class?如何使一个班级成为另一个班级的子组?
【发布时间】:2013-06-02 22:47:44
【问题描述】:
from django.db import models  

class products(models.Model): #Table name, has to wrap models.Model to get the functionality of Django.  

    name = models.CharField(max_length=200, unique=True) #Like a VARCHAR field  
    description = models.TextField() #Like a TEXT field  
    price = models.IntegerField()


    def __unicode__(self): #Tell it to return as a unicode string (The name of the to-do item) rather than just Object.  
        return self.name  


class categories(models.Model):

我是 python 新手,我正在尝试创建一个电子商务商店。正如您在上面看到的,我创建了 products 类,但是在 Categories 类中,我需要包含我创建的所有产品属于某个类别。我不知道该怎么做。

【问题讨论】:

标签: python django class e-commerce


【解决方案1】:

听起来您只需要从 Product 到 Category 的 ForeignKey。

【讨论】:

  • 与教程中的方法相同,其中从 Choice 到 Poll 有一个 ForeignKey。如果您还没有完成教程,请在提问之前先完成。
【解决方案2】:

看看Many to Many

我认为产品可以属于许多类别,因此它应该与类别模型具有多对多关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多