Django models in web2py

56 views
Skip to first unread message

mdipierro

unread,
Nov 16, 2008, 2:22:26 AM11/16/08
to web2py Web Framework
I have ported Django Models to web2py.

This is tentative and needs more testing.

Example:

0) Start web2py and make an app called "yourapp"

1) download http://groups.google.com/group/web2py/web/django.py and
save it in yourapp/modules/django.py

2) create a web2py model yourapp/models/db.py and write this in it:

### web2py specific
from applications.yourapp.modules.django import *
db=SQLDB()
Model=DjangoModelFactory(db,migrate=True)
### end web2py specific

# example of Django model in a web2py model

class Poll(Model):
name=CharField('Name',max_length=32,null=True)
description=TextField(null=True,blank=True)
created_on=DateField(null=True,blank=True)

class Choice(Model):
poll=ForeignKey(Poll)
value=CharField(validator_list=[isLowerCase],choices=((1,1),
(2,2)))

class Person(Model):
name=CharField('Full Name',max_length=64,null=True)
choices=ManyToManyField(Choice)

3) DONE!

The example defines Poll, Choice, Person as web2py SQLTables.

You can access the model via the auto-generated dbadmin interface.

You can use normal web2py syntax

id=Poll.insert(name='test')
for row in db(Poll.id==id).select(): print row.name

You cannot use Django syntax like

p=Poll(name='test') # NO!
p.save() # NO!

Attention:

1) many Django field types and Django validators have been ported but
not all of them

2) the validators that have been ported apply to web2py forms but they
are not exactly identical. For example isValidURL is not quite the
same as IS_URL. The web2py validators are more sophisticated than
Django's (IS_INT_IN_RANGE vs isInteger) but Django has more. Sometime
we do things differently: for example we do not check if isValidHTML,
we just sanitize the output with XML(...,sanitize=True).

3) the ManyToManyField will create the intermediate table, as Django
does.

4) notice that django.py defines a DjangoModelFactory, not a Model
class. The DjangoModelFactory needs a database connection (db) and
needs to know if the tables exist (migrate=False) or not
(migrate=True). If migrate==True, web2py will migrate the tables if
the definition changes. Thus you get Django syntax + web2py migration
power!

5) I am not suggesting using this normally. It is much better in the
long run to use the native web2py API but this may lower the entry
barrier for current Django users.

6) I think I am mapping the right default for the field attributes but
I may have done mistakes.

Please give it a try and let me know if you encounter any problem.

Hopefully Django users and developers will see this an opportunity not
as a threat and will help us improve.

Massimo
Reply all
Reply to author
Forward
0 new messages