The table was defined in rmodels.py. I think I know the reason after
the table was defined were the following lines:
custom_auth_table = db[auth.settings.table_user_name]
auth.settings.table_user = custom_auth_table
auth.define_tables()
Could it be due to auth.define_tables() happening after the table was
defined?
Anyway, I had some help from selecta in the freenode channel.
The functionality I wanted to create is to have a form that allows
users to be created and assigned roles by other users. This is the
code snippet from selecta
user_form = SQLFORM.factory(db.auth_user, Field('role',db.auth_group,
requires = IS_IN_DB(db, 'auth_group.id', '%(role)s',zero=None)))
if user_form.accepts(request.vars, session):
user_id =
db.auth_user.insert(**dict([(fn,user_form.vars.get(fn)) for fn in
db.auth_user.fields]))
if user_form.vars.role:
auth.add_membership(user_form.vars.role, user_id)
return dict(user_form = user_form)
Thanks for the help!
On Oct 21, 11:14 pm, mdipierro <[email protected]> wrote:
> The model files are executed in alphabetical order. Is it possible you
> are defining this table in a file that is executed before db.py?
>
> On Oct 21, 9:59 am, Luther Goh Lu Feng <[email protected]> wrote:
>
>
>
>
>
>
>
> > Yes I tried one at a time but it does not work. And it shows those
> > errors I mentioned.
>
> > On Oct 21, 10:38 pm, mdipierro <[email protected]> wrote:
>
> > > Does it work? You can only have one of the two
>
> > > On Oct 21, 9:15 am, Luther Goh Lu Feng <[email protected]> wrote:
>
> > > > Oops, of course I did change the default as advised:
>
> > > > Field('membership_id', db.auth_membership, default=0),
> > > > Field('membership_id', auth.settings.table_membership, default=0),
>
> > > > On Oct 21, 10:13 pm, Luther Goh Lu Feng <[email protected]> wrote:
>
> > > > > Thanks for the tip.
>
> > > > > I tried the uncommenting the 2 lines separately and got the following
> > > > > errors. Any tips?
>
> > > > > "Field('membership_id', db.auth_membership, default=''),":
>
> > > > > Traceback (most recent call last):
> > > > > File "/home/luther/roverus/gluon/restricted.py", line 188, in
> > > > > restricted
> > > > > exec ccode in environment
> > > > > File "/home/luther/roverus/applications/roverus/models/rmodels.py",
> > > > > line 18, in <module>
> > > > > Field('membership_id', db.auth_membership, default=0),
> > > > > File "/home/luther/roverus/gluon/sql.py", line 1385, in __getattr__
> > > > > return dict.__getitem__(self,key)
> > > > > KeyError: 'auth_membership'
>
> > > > > "Field('membership_id', auth.settings.table_membership, default=''),
> > > > > ":
>
> > > > > Traceback (most recent call last):
> > > > > File "/home/luther/roverus/gluon/restricted.py", line 188, in
> > > > > restricted
> > > > > exec ccode in environment
> > > > > File "/home/luther/roverus/applications/roverus/models/rmodels.py",
> > > > > line 20, in <module>
> > > > > format='%(username)s'
> > > > > File "/home/luther/roverus/gluon/sql.py", line 1365, in define_table
> > > > > t._create(migrate=migrate, fake_migrate=fake_migrate)
> > > > > File "/home/luther/roverus/gluon/sql.py", line 1726, in _create
> > > > > elif field.type.startswith('reference'):
> > > > > AttributeError: 'NoneType' object has no attribute 'startswith'
>
> > > > > On Oct 21, 9:17 pm, mdipierro <[email protected]> wrote:
>
> > > > > > They are both fine but default cannot be a string, must be 0.
>
> > > > > > On Oct 21, 6:21 am, Luther Goh Lu Feng <[email protected]> wrote:
>
> > > > > > > I have the following custom user model. I am trying to reference
> > > > > > > the membership
> > > > > > > type so that when users are created by administrators, they can
> > > > > > > assigned
> > > > > > > different roles. Attention to be drawn to the 2 lines commented
> > > > > > > out at the
> > > > > > > bottom, which I have tried to make the reference but failed. Any
> > > > > > > tips?
>
> > > > > > > db.define_table(auth.settings.table_user_name,
> > > > > > > Field('username', 'string', length=255, required=True,
> > > > > > > requires=IS_NOT_EMPTY(), unique=True, label="Username"),
> > > > > > > Field('first_name', 'string', length=255, required=True,
> > > > > > > requires=IS_NOT_EMPTY(), label="First name"),
> > > > > > > Field('last_name', 'string', length=255, required=True,
> > > > > > > requires=IS_NOT_EMPTY(), label="Last name"),
> > > > > > > Field('password', 'password', length=40, required=True,
> > > > > > > requires=[IS_NOT_EMPTY(),CRYPT(key="sha1:theultimatesuperrover")],
> > > > > > > readable=False, label="Password"),
> > > > > > > Field('last_login', 'datetime', writable=False,
> > > > > > > readable=False,
> > > > > > > update=request.now),
> > > > > > > Field('registration_key', length=512, writable=False,
> > > > > > > readable=False,
> > > > > > > default=''),
> > > > > > > Field('reset_password_key', length=512, writable=False,
> > > > > > > readable=False,
> > > > > > > default=''),
> > > > > > > Field('registration_id', length=512, writable=False,
> > > > > > > readable=False,
> > > > > > > default=''),
> > > > > > > Field('class_id', 'reference classes', default=''),
> > > > > > > #Field('membership_id', db.auth_membership, default=''),
> > > > > > > #Field('membership_id', auth.settings.table_membership,
> > > > > > > default=''),
> > > > > > > format='%(username)s'
> > > > > > > )