views:

45

answers:

2

I have a model with no primary key. it has the id's from other models. I want to call iid_ id.

For example iid_id = 1.

There are 21 rows with the number 1. I want to grab all the rows and display them on a HTML table.

The Model:

class QryDescChar(models.Model):
   iid_id = models.IntegerField()
   cid_id = models.IntegerField()
   cs = models.CharField(max_length=10)
   cid = models.IntegerField()
   charname = models.CharField(max_length=50)
   class Meta:
       db_table = u'qry_desc_char'

This is the SQL for the Model:

CREATE VIEW qry_desc_char as
SELECT  tbl_desc.iid_id,
        tbl_desc.cid_id,
        tbl_desc.cs,
        tbl_char.cid,
        tbl_char.charname
FROM tbl_desc, tbl_char 
WHERE tbl_desc.cid_id = tbl_char.cid;

the view:

def itemdetail(request, iid):
     i = get_object_or_404(Item, pk=iid) #this is from another model
     row = ????????

     return render_to_response('tbl/itemdetail.html', {'item': i, 'row': row})

I have tried everything and i cant get the rows on the HTML to display. This might be a stupid question. Im new at Django and cant make this work.

+1  A: 

If the rows are child objects from the item, just do this..

row = i.qrydescchar_set.all()

That assumes your model for the item has a relation indicated to the QryDescChar

SleighBoy
A: 

I'm not sure if I understand your question... Do you want to get all QryDescChar objects with iid_id equal to primary key of Item you have? Something like this:

#from somewhere.models import QryDescChar
def itemdetail(request, iid):
    i = get_object_or_404(Item, pk=iid) #this is from another model
    row = QryDescChar.objects.filter(iid_id=i.pk) 

    return render_to_response('tbl/itemdetail.html', {'item': i, 'row': row})

Then you can iterate over the row in template:

<table>
  {% for obj in row %}
    <tr>
      <td>{{ obj.cs }}</td>
      <td>{{ obj.charname }}</td>
      {# whatever you want to display in a row #}
    </tr>
  {% endfor %}
</table>
Daniel Hernik