使用视图显示模型对象

时间:2016-05-31 04:24:39

标签: python django django-queryset manytomanyfield

我是Django的新手,想知道如何从django数据库中检索记录。由于我在学习,我不知道我创建的模型是否正确。

model.py

class ProductCategory(models.Model):
    prodname = models.CharField(_("Product Name"), max_length=128, primary_key=True)
    prodowner = models.CharField(_("Product Owner Name"), max_length=100)
    email_message_id = models.CharField(_("e-mail message ID"), max_length=255, blank=True, null=True)
    dependency = models.ManyToManyField(ThirdpartyProduct, related_name='tplibcategories')

    def __unicode__(self):
        return u'%s' % ( self.prodname )

这是我的产品表,其中有许多字段指向thirdpartyProduct表,要求是每个产品都对第三方库有多重依赖,因此产品可以依赖于多个组件,组件也可能成为产品。

ThirdparyproductTable

class ThirdpartyProduct(models.Model):
    tplib_name = models.CharField(_("Library Name"), max_length=128)
    tplib_type = models.CharField(_("Library Type"), max_length=10 choices=LIBTYPE, default='LIB')


    def __unicode__(self):
        return u'%s' % ( self.tplib_name )  

我的views.py

当我尝试显示依赖项时,它只显示我的第三方组件,我不知道如何制定查询集,以便我可以获取产品名称及其所有第三方组件详细信息。

class MyThirdpartyView(ListView):
    model = ThirdpartyProduct
    template_name = 'thirdpartyproduct_list.html'

base.html文件

<h1>ThirdParty</h1>
<ul>
    {% for tp in object_list %}
    <li class="thirdparty">{{ tp }}</li>
    {% endfor %}
</ul>

我知道存在一些问题,但我需要您的宝贵意见和一些可行的解决方案。

0 个答案:

没有答案