简体   繁体   English

Django在视图中获取自引用ManyToManyField中的相关项目

[英]Django getting related items in self-referential ManyToManyField in view

How do I get all the items associated with a part via the self-referential ManyToManyField? 如何通过自引用的ManyToManyField获取与零件关联的所有项目? How to I fix my view to get part_list to contain a list of all the parts associated with 'product' and in the order specified by order_by? 如何修复我的视图,使part_list包含与“产品”关联的所有零件的列表,并按order_by指定的顺序排列?

# views.py

def productdetail(request, slug):    
    product = get_object_or_404(PartModel, slug=slug)

    part_list = PartModel.objects.all().filter(buildpart__id=product.pk).order_by('family__type')

    return render(request, 'productdetail.html', locals())

Here's the template: 这是模板:

# productdetail.html

    <header>
        <h1>'{{ product.name }}' Detail Page</h1>
    </header>

    <p>{{ product.name }}
    <p>{{ product.slug }}
    <p>{{ product.family }}
    <p>{{ product.family.type }}
    <p>{{ product.family.make }}
    <p>${{ product.price }}
    {% for part in part_items %}
        <p>{{ part.name }}
    {% endfor %}

Notice the PartModel model holding our inventory and its self-referential BuildPart ManyToMany model through the buildpart field: 注意通过buildpart字段保存我们的库存的PartModel模型及其自引用的BuildPart ManyToMany模型:

class PartModel(models.Model):
    family = models.ForeignKey(PartFamily)
    name = models.CharField("Model Name", max_length=50, unique=True)
    buildpart = models.ManyToManyField('self', through='BuildPart',
                                symmetrical=False, related_name='+')

class Build(models.Model):
    build = models.ForeignKey(PartModel, related_name='+')
    part = models.ForeignKey(PartModel, related_name='+')
    quantity = models.PositiveSmallIntegerField(default=1)

    class Meta:
        abstract = True
        unique_together = ('build', 'part')

    def __unicode__(self):
        return self.build.name + ' with ' + str(self.quantity) + ' * ' + \
               self.part.family.make.name + ' ' + self.part.name

class BuildPart(Build):
    pass

    class Meta:
        verbose_name = "Build Part"

To get everything in the right order with the order_by clause we follow the 'family' field to the PartFamily model: 为了使用order_by子句以正确的顺序获取所有内容,我们遵循PartFamily模型的“ family”字段:

class PartFamily(models.Model):
    make = models.ForeignKey(PartMake)
    type = models.ForeignKey(PartType)
    name = models.CharField("Family Name", max_length=30,
                             unique=True)
    slug = models.SlugField(unique=True)

And lastly, we get to the model with the 'order' field, the one we wish to sort the related items by, PartType: 最后,我们进入带有“订单”字段的模型,我们希望通过PartType对相关项目进行排序:

class PartType(models.Model):
    name = models.CharField("Part Type", max_length=30, unique=True)
    slug = models.SlugField(unique=True)
    order = models.PositiveSmallIntegerField()
    description = models.TextField(blank=True, null=True)

I was able to find an answer to my own question with Roseman's help. 在罗斯曼的帮助下,我能够找到自己的问题的答案。 The part_list line should be as follows: part_list行应如下所示:

# views.py

part_list = product.buildpart.all().order_by('family__type')

Once Roseman pointed out the variable name mismatch I did up the QuerySet as it made the most sense to me, and it works! 一旦罗斯曼指出变量名不匹配,我就对QuerySet进行了修改,因为它对我来说最有意义,并且可以!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM