0

I have been learning django from few days. I have couple of models , and these perfectly show up on the admin site , once i register the models in admin.py. But i want to create database view (not the view which django refers) , i have created the model for the view, with managed = False on models.py . this database view is a join of two tables. and when i start my server, i get error some saying that my database view is not a table , which is correct. But I am missing some thing here that i unable to solve this. what might be that i am missing. or is my whole idea of database view itself wrong , if so how do i do this - is it allays , that i have to run sql( of course using django db api).

Heres my code.

class product(models.Model):
    users = models.ForeignKey(User)
    product = models.CharField(max_length=20)

     def __unicode__(self):
         return self.product


class product_models(models.Model):
     product = models.ForeignKey('product')
     model_name = models.CharField(max_length=50)
     model_price = models.IntegerField(max_length=4)
     model_desc = models.TextField(blank=True)
     commision = models.IntegerField(max_length=3)
     def __unicode__(self):
         return self.model_name   

class my summary_view(model.Model):
# this is my database view
    product = models.ForeignKey('product')
    model_name = models.ForeignKey('product_models')


    class meta :
          managed = False

"""
I am assuming this is what above code does, I may be wrong also here in creating
database view in the above table . 

Create View `summary_view` as 
Select
    p.product, m.model_name
From
    products p , product_model m. 
"""
rgm
  • 1,241
  • 2
  • 16
  • 33
  • show us your code!! theoretical explanations are nonesense if we can't tell what you did right\wrong – yuvi Oct 23 '13 at 18:51
  • I don't get what you're trying to do. Is it supposed to be a basis for a dynamic model? Or is summery supposed to be a mediatery model? I'm really confused. How is your model a view exactly? those are two completely different concepts! – yuvi Oct 23 '13 at 19:21
  • @yuvI have added more code. – rgm Oct 23 '13 at 19:27
  • Your general approach seems OK, but there are some specific problems. First, I don't think you want `ForeignKey` fields in the Django model for your view. Your SQL is selecting fields that are `CharField`s in Django. Secondly, you've omitted your `join` or `where` constraint from your view creation sql. If your ultimate goal is to have the admin handle updates to that view, I've never tested that exact interaction - it might not be supported. But I don't know that it's not, I've just never tried. Unmanaged models are a common way to read data from database views in Django. – Peter DeGlopper Oct 23 '13 at 19:52
  • While I have not tested this, should the view name be appname_summary_view in the database? Or do you need to use the meta db_table = 'tablename' – ModulusJoe Oct 23 '13 at 20:39
  • 1
    Yes, you need to make the view name in the database and the name Django looks for match up through one of those approaches. – Peter DeGlopper Oct 23 '13 at 20:42

1 Answers1

0

Your view is called summary_view, but django would by default look for appname_summary_view, either:

Add

class meta:
  db_table = 'summary_view'
  managed = False

Or update your view

create view 'appname_summary_view'...
ModulusJoe
  • 1,416
  • 10
  • 17
  • your answer sort of worked. thanks , but i do not wish to insert data, just view which is already present in db, with this. what can i do further from here. I did `manage.py sqlall`. this actually created table with `create table` instead of `create view`. – rgm Oct 23 '13 at 21:13
  • I don't think django by default supports the view concept in the ORM. Some other stack trace posts talk about over riding the save and delete methods: http://stackoverflow.com/questions/12158463/how-can-i-make-a-model-read-only – ModulusJoe Oct 23 '13 at 21:22
  • I also don't know what you are eventually trying to achieve, your view itself looks a little limited. Is there more to your view than you have posted? – ModulusJoe Oct 23 '13 at 21:24
  • what i have posted is just a sample of what i want, my db is more with lot of cols and my view is of more cols. but that apart , is my approach for view is correct , what i meant is should i be creating view here in the first place or just a run a query when ever a requests comes. – rgm Oct 23 '13 at 21:32
  • There is a good post on query vs view here http://stackoverflow.com/questions/10302615/mysql-views-performance/10379846#10379846 (personally I haven't had good experiences with views and just use joins where needed) – ModulusJoe Oct 23 '13 at 22:12