Web2py -------------- example of using web2py to write django, web2pydjango
Model Creation
Under models and where polls. py
The file content is as follows:
1 #-*-coding: UTF-8-*-2 pollsdb = DAL (uri = 'sqlite: // polls. db') 3 4 pollsdb. define_table ('Question ', 5 Field ('Question _ text', 'string', length = 200), 6 Field ('pub _ date', 'datetime '), 7 format = '% (question_text) s') 8 9 pollsdb. define_table ('choice ', 10 Field ('Question', 'reference question ', requires = IS_IN_DB (pollsdb, 'Question. id', pollsdb. question. _ format), 11 Field ('choice _ text', 'string', length = 200), 12 Field ('votes ', 'integer', default = 0 ), 13 format = '% (choice_text) s ')Polls. py
Detailed sales details I wrote above to facilitate Paste
To explain, pollsdb is a database file. The official DAL is an independent plug-in that supports many databases. Here sqlite is used.
Create two data tables, Question and Choice.
The Choice table has the question field as the foreign key.
We can log on to the background management to see
Enter http: // 127.0.0.1/polls/appadmin/
Enter the management password
We can see the two tables we have created and the system permission table, but it does not matter for this application currently.
We can add new records for the Question table.
Click the new record button
We can see that the datetime data is web2py, which provides a plug-in for us to select the time.
Enter and click Submit
Click the table name on the management interface to view the list.
It is indeed a simple management interface, but developers do not care about these details
Let's take a look at the Choice add page in the same way.
It should be a perfect presentation. The system has made a foreign key Association.
The model creation here is over.
Exercises after class
Try to use different databases
The system provides us with many verification tools. You can try it. Here we only use IS_IN_DB