Вы находитесь на странице: 1из 9

Database Views in django

fri f Ja i Al raf a Sh By: Syed

swits Bit

Overview

In database theory, a view consists of a stored query accessible as a virtual table in a relational database or a set of documents in a document-oriented database composed of the result set of a query or map and reduce functions. Unlike ordinary tables (base tables) in a relational database, a view does not form part of the physical schema: it is a dynamic, virtual table computed or collated from data in the database. Changing the data in a table alters the data shown in subsequent invocations of the view.

Views can provide advantages over tables: Views can represent a subset of the data contained in a table Views can join and simplify multiple tables into a single virtual table Views can act as aggregated tables, where the database engine aggregates data (sum, average etc.) and presents the calculated results as part of the data Views can hide the complexity of data; for example a view could appear as rate_tata_2jan or rate_tata_25jan, transparently partitioning the actual underlying table Views take very little space to store; the database contains only the definition of a view, not a copy of all the data it presents Depending on the SQL engine used, views can provide extra security Views can limit the degree of exposure of a table or tables to the outer world

Let Suppose

A Database View is a subset of the database sorted and displayed in a particular way. For example, in an RMS database, perhaps you only wish to display the Received date with company name stored in the database. To do that you would create a sheets view. The RMS database has for each Sheet Rate, Destination, Company, Received Date etc.

Create Model

First you have to create Model with required Fields.

This actually helps to use View in django admin app list and utilize admin functions. This tells Model not to create table and define mapping to View This also override Save function of django, since views are readonly.

Add Meta to Model:

Sample Model

class RateSheetsdates(models.Model): sheet = models.CharField(max_length = 1024, primary_key = True) Company = models.CharField(max_length = 552) rcv_date = models.DateField() class Meta: managed = False db_table = u'RateManagement_RateSheetsdates' unique_together = (('Company', 'rcv_date'),) verbose_name = 'Rate Sheet Datewise' verbose_name_plural = 'Rate Sheets Datewise' def save(self, **kwargs): raise NotImplementedError

Create DB View

2nd Step is to create View for table Now you have to write Query to fill view for fields you required then Execute this. As of writing Model it execute once. To test Query go to Next Page...

Create View Sample

Import sqlite3 conn = sqlite3.connect('/home/sharafjaffri/Desktop/bitswits1/bitswits/generate.db') c = conn.cursor() sql = 'DROP VIEW RateManagement_RateSheetsdates' try: c.execute(sql) except: pass sql2 = "CREATE VIEW RateManagement_RateSheetsdates AS SELECT company_id || '#' || rcv_date as sheet, company_id AS Company, rcv_date FROM RateManagement_rate" try: c.execute(sql2) except: pass conn.commit()

Control View in django Admin

How to Control Model Representation and accessibility in admin.py file of your app is already described by Mr Ali Raza. Since Database Views are ready only and we have to remove delete and Add/Change option in changelist. So let go straight toward admin file. Open RateManagement/admin.py

Thanks for you time.

Вам также может понравиться