views:

212

answers:

3

this is a model of the view table.

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 i use to create the table

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;


i dont know if i need a function in models or views or both. i want to get a list of objects from that database to display it. This might be easy but im new at Django and python so i having some problems

A: 

You are trying to fetch records from a view. This is not correct as a view does not map to a model, a table maps to a model.

You should use Django ORM to fetch QryDescChar objects. Please note that Django ORM will fetch them directly from the table. You can consult Django docs for extra() and select_related() methods which will allow you to fetch related data (data you want to get from the other table) in different ways.

shanyu
A: 

If your RDBMS lets you create writable views and the view you create has the exact structure than the table Django would create I guess that should work directly.

gnrfan
+1  A: 

Django 1.1 brought in a new feature that you might find useful. You should be able to do something like:

 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'
     managed = False

The documentation for the managed Meta class option is here. A relevant quote:

If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed is False. All other aspects of model handling are exactly the same as normal.

Once that is done, you should be able to use your model normally. To get a list of objects you'd do something like:

qry_desc_char_list = QryDescChar.objects.all()

To actually get the list into your template you might want to look at generic views, specifically the object_list view.

Monika Sulik