(web2py) add extra fields auth_user that reference another field - authentication

I am using Web2py and I would like to add extra fields in the auth_user. some of these fields are reference to other table. for example:
auth.settings.extra_fields['auth_user']= [
Field('country', 'reference countries')]
db.define_table(
'countries',
Field('name'),
format = '%(name)s'
)
but I receive this issue:
cannot resolve reference countries in auth_user definition
can any one help me what should I do? how can I link auth_user table with another table???
All the Best

you need to make sure your db.define_table is created before your the auth tables
like this :
db.define_table('bank',
Field('name'),
format = '%(name)s')
auth.settings.extra_fields['auth_user'] =
[Field('bank', 'reference bank',
label = T('Bank'),
notnull = True,
required = True,
requires = IS_IN_DB(db, db.bank.id, '%(name)s') ),
]
auth.define_tables(username = True, signature = True)
custom_auth_table = db[auth.settings.table_user_name]
auth.settings.table_user = custom_auth_table

Related

How to create a record for field using many2one in Odoo 13?

I have 3 models. I used One2many field o put the tree view of model 3 in form view of model 1 and 2.
1/ bao_hiem.py with one2many field:
lstd_baohiem = fields.One2many('lich.su', 'name')
thamchieu = fields.Char('Tham chiếu')
2/ dieu_chinh.py with one2many field:
lstd_dieuchinh = fields.One2many('lich.su', 'name')
thamchieu = fields.Char('Tham chiếu')
And 3/ lich_su.py with many2one field:
name = fields.Many2one('bao.hiem')
name_id = fields.Many2one('bao.hiem')
thamchieu = fields.Char('Tham chiếu')
I want to pass the values (eg: 'name', 'thamchieu') from dieu_chinh.py to lich_su.py and auto record with all those values via a button. So that, in the user's form view of model 1 and 2 can show the recorded value also, called the activity history of 1 user.
The code for the button like this:
def chapthuan(self):
giatri_lstd = self.env['lich.su']
gt = {
'name' : self.name.id,
'thamchieu' : self.thamchieu,
'thoigian' : self.thoigian
}
list_lstd_dieuchinh=[]
for line in self.lstd_dieuchinh :
art = {}
art['name'] = line.name.id
art['lstd_dieuchinh'] = line.lstd_dieuchinh
art['thamchieu'] = line.thamchieu
art['thoigian'] = line.thoigian
list_lstd_dieuchinh.append((0, 0, art))
gt.update({ # add the list of command to gt
'thamchieu': list_lstd_dieuchinh
})
giatri_lstd = giatri_lstd.create(gt)
return True
After click the button 'chapthuan', it can pass the value and create the record with the field 'name', 'date'. But with the field: 'thamchieu' which has relation many2one with model2 is still not worked.
I must select manually in the form view of the user.
So how to create a record with many2one field?
Please help!
Thank you
https://www.odoo.com/documentation/13.0/reference/orm.html#create-update
You are trying to create records by (0, 0, {values}) to name_id by the way of x2many field.
But name_id is Many2one field. That's your programming error.
But the error log you showed happened when you are trying to remove a record that already links to another record. So what I said above is not the main problem... Anyway, I'm a little confused with your code, it looks not so good

Erlang ets:select sublist

Is there a way in Erlang to create a select query on ets table, which will get all the elements that contains the searched text?
ets:select(Table,
[{ %% Match spec for select query
{'_', #movie_data{genre = "Drama" ++ '_' , _ = '_'}}, % Match pattern
[], % Guard
['$_'] % Result
}]) ;
This code gives me only the data that started (=prefix) with the required text (text = "Drama"), but the problem is I need also the the results that contain the data, like this example:
#movie_data{genre = "Action, Drama" }
I tried to change the guard to something like that -
{'_', #movie_data{genre = '$1', _='_'}}, [string:str('$1', "Drama") > 0] ...
But the problem is that it isn't a qualified guard expression.
Thanks for the help!!
It's not possible. You need to design your data structure to be searchable by the guard expressions, for example:
-record(movie_data, {genre, name}).
-record(genre, {comedy, drama, action}).
example() ->
Table = ets:new('test', [{keypos,2}]),
ets:insert(Table, #movie_data{name = "Bean",
genre = #genre{comedy = true}}),
ets:insert(Table, #movie_data{name = "Magnolia",
genre = #genre{drama = true}}),
ets:insert(Table, #movie_data{name = "Fight Club",
genre = #genre{drama = true, action = true}}),
ets:select(Table,
[{#movie_data{genre = #genre{drama = true, _ = '_'}, _ = '_'},
[],
['$_']
}]).

How to define display order of edit view in Flask appbuilder?

According to document of flask appbuilder, the display order of Model detail view can be defined as following:
class DeviceView(ModelView):
datamodel = SQLAInterface(Device)
related_views = [ApplicationView, EventView]
label_columns = {'snr':'SNR', 'api_dev_id':'Device ID', 'api_dev_key':'Device Key','owner_id':'Owner'}
list_columns = ['name','snr','addr','owner']
show_fieldsets = [
('Summary',
{'fields':['name','snr','owner']}
),
('Device Info',
{'fields': ['addr','latitude','longitude','status','api_id','api_key','api_dev_id','api_dev_key'],'expanded':True}
),
]
And it works for detail view. But I can not find the definition for edit view. Here is my snapshot for Device edit page. The order seems random.
Have I missed any part inside the document?
I found the answer from another question:
Ordering data in a Flask Appbuilder view
class RegistrationTestView(ModelView ):
datamodel = SQLAInterface(RegistrationTest)
name = "RegistrationTestView"
page_size=1000
label_columns = {'CarNo':'Car #', 'FirstName':'First Name','LastName':'Last Name'}
list_columns = ['CarNo','FirstName','LastName']
edit_columns = ['FirstName','LastName']
show_columns = edit_columns
add_columns = edit_columns
order_columns = ['FirstName']
Display in edit/list/show are ordered seperately.

how to create an index in mongoengine to be unique=True and sparse=True

I am using mongoengine with flask. I have a db.Document class called profile in which i want a field to be nullable and unique, i understand the way to do this is to make an index of that field that is both sparse=True and unique=True, how do i go about doing this?
You will have to declare the index in the meta definition eg:
class BlogPost(Document):
date = DateTimeField(db_field='addDate', default=datetime.now)
category = StringField()
tags = ListField(StringField())
meta = {
'indexes': [
{'fields': ['-date'], 'unique': True,
'sparse': True, 'types': False },
],
}
In case of unique constraint you can set it with the field declaration as:
email = mongodb.EmailField(required=True, unique=True)
We can directly mention it in the Field parameters. Example:
email = db.EmailField(sparse=True, unique=True, required=False)

How to map a many-to-many linking table to use another schema?

I have a few classes that I map to non-default schema using NHibernate.Mapping.Attributes like this:
[Class(Lazy = true, Schema = "NonDefaultSchemaName")]
public class Class1
I also have some many-to-many mappings:
[Set(Inverse = false, Table = "Class1ToClass2", Lazy = true, Cascade = "none")]
[Key(1, Column = "Class1ID")]
[ManyToMany(2, ClassType = typeof(Class2), Column = "Class2ID")]
What I am trying to achieve is to have the linking table Cass1ToClass2 mapped to
[NonDefaultSchemaName].[Class1ToClass2]
NHibernate automatically maps to the default schema. I could not find anything similar googling around...
should be possible now using
[Set(Inverse = false, Table = "Class1ToClass2", Schema = "Schema", Lazy = true, Cascade = "none")]