0

I am new to Django and wanted to know how to retrieve a record from django database. Since I am learning, I don't know if model created by me is correct or not.

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 )

This is my product table which has many to many field pointing to thirdpartyProduct table, The requirement is each product has multiple dependency on thirdparty library so a product can dependent on multiple component and it might possible that component can also become Product.

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 )  

My views.py

When I try display dependency it only shows my thirdparty components, I dont know how to formulate queryset so that I can get Product name and its all thirdparty component details.

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>

I know some issue is there, but I need your valuable comments and some workable solution.

solarissmoke
  • 30,039
  • 14
  • 71
  • 73
Ajay k
  • 1
  • 2
  • Is the Django template code you posted found in base.html, as indicated, or within the thirdpartyproduct_list.html template, as your ListView would indicate it should be? – Jack Shedd May 31 '16 at 04:27
  • yes its the same. I need to understand if my class and requirement are cleared enough. Second how can I populate these object using queryset. which I can made this available to base.html or _list.html – Ajay k May 31 '16 at 05:12
  • This is why I hate class based views. For a newbie there isn't an easy place to add debug information to figure out what might be going wrong. Can you do ./mange.py shell and see what ThirdpartyProduct.objects.count() gives you? it's possible that your table is actually empty – e4c5 May 31 '16 at 05:14
  • Since I have added entry through admin I know for sure that entries are present. Also using ./manage shell gives me count() of 36 for a id=1, 2 for id=2 – Ajay k May 31 '16 at 05:30
  • Is my assumption of relationship are correct so that I am well assured that things ok at one level. – Ajay k May 31 '16 at 05:31
  • @Ajayk: you are missing `queryset` class variable in `MyThirdPartyView`. Add `queryset = ThirdpartyProduct.objects.all()` – Amrullah Zunzunia May 31 '16 at 10:44
  • I need not explicitly add query set as my Product table already has dependecy field which is ManyToManyField for Thirdpartyview. – Ajay k Jun 01 '16 at 03:53

0 Answers0