首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何创建类别和子类别

如何创建类别和子类别
EN

Stack Overflow用户
提问于 2019-12-21 12:03:17
回答 3查看 98关注 0票数 0

这是我的项目模型,我如何创建一个类别和子类别,如:男装衬衫和女装衬衫。

代码语言:javascript
运行
复制
class Item(models.Model):
    title = models.CharField(max_length=250)
    company_name = models.CharField(max_length=50, blank=True)
    product_you_may_like = models.ForeignKey(ProductYouMayLike, null=True, blank=True, 
    on_delete=models.CASCADE)
    image = models.ImageField(null=True)
    price = models.IntegerField()
    old_price = models.IntegerField(null=True, blank=True)
    shipping_amount = models.IntegerField()
    percentage_off = models.IntegerField(null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    specification = models.TextField(null=True, blank=True)
    product_type = models.CharField(max_length=20, choices=PRODUCT_TYPE, null=True, blank=True)
    slug = AutoSlugField(populate_from='title', unique=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ['-timestamp']
        db_table = 'items'
        verbose_name_plural = 'Items'

    def __str__(self):
        return self.title
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-12-21 16:12:21

我通过在项目模型中添加一个模型类别和一个外键来解决这个问题。例如,

代码语言:javascript
运行
复制
class Item(models.Model):
    category = models.ForeignKey(Category,     null=True,blank=True,
                             on_delete=models.CASCADE)

class Category(models.Model):
    name = models.CharField(max_length=250)  

在模型中添加类别中的men_shirt,然后保存。

在views.py中:

代码语言:javascript
运行
复制
def template(request):
    category = Category.objects.filter(name="men_shirt")
    context = { 'category':category } 
    return render(request, "template.html", context) 

在模板中:

代码语言:javascript
运行
复制
{% for category in category %} 
{% for item in category.item.all %}
 <h1>This will display all content in item by the name you filtered in views.py </h1>
{% endfor %} 
{% endfor %} 
票数 0
EN

Stack Overflow用户

发布于 2019-12-21 12:13:12

您可以使用吉安戈-创建一个类别树,然后在项目模型中拥有该模型的外键。

代码语言:javascript
运行
复制
class Category(MPTTModel):
    name=models.CharField(max_length=75,null=False,blank=False, unique=True)
    parent=TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True, on_delete=models.CASCADE)
票数 1
EN

Stack Overflow用户

发布于 2019-12-21 12:12:38

我不知道你是怎么写你的MenShirtCategory的,但是这个名字让我觉得你混淆了类和实例。

通常,您可能有一个Category模型(或者ItemCategory,如果这些履带特定于Item)是这样写的:

代码语言:javascript
运行
复制
class Category(models.Model):
    name = models.CharField(max_length=..., ...)  # men shirts will be the name
    parent = models.ForeignKey('self', null=True, blank=True,
                               on_delete=...,
                               related_name='children')

然后将其连接到项目模型:

代码语言:javascript
运行
复制
class Item(models.Model):
    category = models.ForeignKey(Category, null=True, blank=True,
                                 on_delete=models.CASCADE)

通过在'self'模型的parent字段中使用Category,您可以实现嵌套类别的概念。

当然,您可能需要手动确保不创建循环(A的B子代),因为数据库(然后是模型)无法为您创建循环。在执行此操作时,您还可能需要检查某个类别不能是它自己的父级。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59436008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档