The example is very old and too many changes neede.
Even after the deprecated fixes , DjangoModelFactory not working anymore

it have this error:

Traceback (most recent call last):
  File "/home/v3ss/workspace/web2py/gluon/restricted.py", line 224, in
restricted
    exec ccode in environment
  File "applications/otwopy/models/db.py", line 91, in <module>
    class Poll(Model):
  File "applications/otwopy/modules/django.py", line 229, in __new__
    table = db.define_table(name, migrate=migrate, *fields)
  File "/home/v3ss/workspace/web2py/gluon/dal/base.py", line 787, in
define_table
    table = self.lazy_define_table(tablename,*fields,**args)
  File "/home/v3ss/workspace/web2py/gluon/dal/base.py", line 804, in
lazy_define_table
    table = table_class(self, tablename, *fields, **args)
  File "/home/v3ss/workspace/web2py/gluon/dal/objects.py", line 327, in __init__
    'define_table argument is not a Field or Table: %s' % field)
SyntaxError: define_table argument is not a Field or Table: <no table>.name

.

On Sat, Mar 28, 2015 at 3:26 AM, Ron Chatterjee [email protected]
<http://mailto:[email protected]> wrote:

This is an old web2py blog.

http://www.web2py.com/AlterEgo/default/show/189

Did anyone went through the steps and was able to generate the tables? I
tried but it failed. It didn’t work. Is it only valid for polls example only
or any django models can be used in web2py this way? Not sure.

I know its not a recommended and I will never deploy an application using
this method. But wanted to try few django projects and code them up in
web2py to learn more but I am and kind of feeling lazy to write the table
from scratch. thought copy and paste will be nice if I can get this method
to work. Any web2pier wants to try and see what I am doing wrong?

I get an error saying:

models/db.py”, line 62, in
class Poll(Model):
File “applications\try





*djangopolls\modules\django.py”, line 145, innew
fields=[db.Field(key,**value.serial(name,db)) for key,value inattrs.items()
if hasattr(value,’serial’) and notisinstance(value,ManyToManyField)] File
“…\Desktop\web2py_src\web2py\gluon\dal\base.py”, line 893, in__getattr*
return ogetattr(self, key)

AttributeError: ‘DAL’ object has no attribute ‘Field’

—
Resources:

   - http://web2py.com
   - http://web2py.com/book (Documentation)
   - http://github.com/web2py/web2py (Source code)
   - https://code.google.com/p/web2py/issues/list (Report Issues)

------------------------------

You received this message because you are subscribed to the Google Groups
“web2py-users” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
For more options, visit https://groups.google.com/d/optout.

​

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
from gluon.sql import DAL
from dal import Field
from gluon.validators import *

isAlphaNumeric = IS_ALPHANUMERIC()
isAlphaNumericURL = IS_URL()
isSlug = IS_MATCH(r'^[-\w]+$')
isLowerCase = IS_LOWER()
isUpperCase = IS_UPPER()
isValidIPAddress4 = IS_MATCH(r'^\d{1,3}(\.\d{1,3}){3}$')
isNotEmpty = IS_NOT_EMPTY()
isOnlyDigits = IS_MATCH(r'^\d*$')
isNotOnlyDigits = IS_MATCH(r'^[^\d]*$')
isInteger = IS_INT_IN_RANGE(-10 ** 10, 10 ** 10)
isOnlyLetters = IS_MATCH(r'^[a-zA-Z]*$')
isValidANSIDate = IS_DATE()
isValidANSITime = IS_TIME()
isValidEmail = IS_EMAIL()
isValidPhone = IS_MATCH(r'^\+?\d{0,2}\-?\d{3}\-?\d{3}\-?\d{4}$')
isValidURL = IS_URL()

# Undefined validators

# isValidImage
# isValidImageURL
# isValidQuicktimeVideoURL
# isValidHTML
# isWellFormedXml
# isWellFormedXmlFragment
# isExistingURL
# isValidUSState
# hasNoProfanities


class DjangoField:

    def __init__(self, *a, **b):
        self.a = a
        self.b = b
        self.validators = []
        self.attributes = {}
        if a:
            self.attributes['label'] = str(a[0])
        if b.get('null', False) == False:
            self.attributes['notnull'] = True
        if b.get('blank', False) == False:
            self.attributes['required'] = True
            self.validators.append(IS_NOT_EMPTY())
        if b.get('default', None):
            self.attributes['default'] = b['default']
        if b.get('unique', False):
            self.attributes['unique'] = True
        if b.get('max_length', None):
            length = self.attributes['length'] = b['max_length']
            self.validators.append(IS_LENGTH(length))
        if b.get('verbose_name', None):
            self.attributes['label'] = b['verbose_name']
        for v in b.get('validator_list', []):
            self.validators.append(v)
        if b.get('choices', None) != None:
            self.validators = IS_IN_SET(
                [x for x, y in b['choices']], [y for x, y in b['choices']])
        self.attributes['requires'] = self.validators


class CharField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        return dict(type='string', **self.attributes)


class TextField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        return dict(type='text', **self.attributes)


class IntegerField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)
        self.attributes['requires'].append(
            IS_INT_IN_RANGE(-10 ** 10, 10 ** 10))

    def serial(self, name, db):
        return dict(type='integer', **self.attributes)


class FloatField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)
        self.attributes['requires'].append(
            IS_FLOAT_IN_RANGE(-10 ** 10, 10 ** 10))

    def serial(self, name, db):
        return dict(type='double', **self.attributes)


class BooleanField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        return dict(type='boolean', **self.attributes)


class DateField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)
        self.attributes['requires'].append(IS_DATE())

    def serial(self, name, db):
        return dict(type='date', **self.attributes)


class DateTimeField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)
        self.attributes['requires'].append(IS_DATETIME())

    def serial(self, name, db):
        return dict(type='datetime', **self.attributes)


class TimeField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)
        self.attributes['requires'].append(IS_TIME())

    def serial(self, name, db):
        return dict(type='time', **self.attributes)


class SlugField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)
        self.attributes['requires'].append(IS_APHANUMERIC())

    def serial(self, name, db):
        return dict(type='string', **self.attributes)


class FileField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        return dict(type='upload', **self.attributes)


class ImageField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        return dict(type='upload', **self.attributes)


class EmailField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        self.attributes['requires'].append(IS_EMAIL())
        return dict(type='string', **self.attributes)


class URLField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        self.attributes['requires'].append(IS_URL())
        return dict(type='string', **self.attributes)


class ForeignKey(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db):
        other = str(self.a[0])
        self.attributes['requires'] = IS_IN_DB(
            db, db[other].id, db[other][db[other].fields[1]])
        return dict(type='reference ' + other, **self.attributes)


class ManyToManyField(DjangoField):

    def __init__(self, *a, **b):
        DjangoField.__init__(self, *a, **b)

    def serial(self, name, db, migrate):
        other = str(self.a[0])
        db_name = self.b.get('db_name', '%s__%s' % (name, other))
        db.define_table(db_name, db.Field(name, db[name]), db.Field(
            other, db[other]), migrate=migrate)
        self.attributes['requires'] = IS_IN_DB(
            db, '%s.%s' % (db_name, name), '%(id)s')
        return dict(type='reference ' + db_name, **self.attributes)


def DjangoModelFactory(db, migrate=True):
    class DjangoModelMetaClass(type):

        def __new__(cls, name, bases, attrs):
            obj = type.__new__(cls, name, bases, attrs)
            if name != 'DjangoModel':
                db = obj._db
                fields = [Field(key, **value.serial(name, db)) for key, value in attrs.items(
                ) if hasattr(value, 'serial') and not isinstance(value, ManyToManyField)]
                table = db.define_table(name, migrate=migrate, *fields)
                fields = [value.serial(name, db, migrate) for key, value in attrs.items(
                ) if not name in db.tables and isinstance(value, ManyToManyField)]
                return table
            return obj

    class DjangoModel(object):
        __metaclass__ = DjangoModelMetaClass
        _db = db

        def __init__(self, **values):
            table = self.TABLE
            for key in table.fields:
                self.__setattr__(key, table[key].default)
            for key, value in values.items():
                if key == 'id' or not key in table.fields:
                    raise SyntaxError, "invalid field"
                self.__setattr__(key, value)

        def __setattr__(self, key, value):
            if not key in self.TABLE.fields:
                raise SyntaxError, "invalid field"

        def save(self):
            table = self.TABLE
            fields = {}
            for key in table.fields:
                if key == 'id':
                    continue
                fields[key] = self.__getattribute__(key)
            table.insert(**fields)
    return DjangoModel


def test_django_model():
    db = SQLDB()
    Model = DjangoModelFactory(db, migrate=True)

    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)

    Poll.insert(name='text')
    for row in db(Poll.id > 0).select():
        print row
    db.commit()

    print Poll.created_on.requires
    print Choice.value.requires

if __name__ == '__main__':
    test_django_model()

Reply via email to