콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
8 답글
6414 화면

Hi guys,

I'm trying to check if there already is in database brand name ... but it won't work. Would you help me with?

======= c o d e =======

class product_brand(orm.Model):

_name = 'product.brand'

_sql_constraints = [('name_uniq','Check(1=1)','Brand name must be unique!'),]             # is this correct ?

_columns = {

'name': fields.char('Brand'),

'description': fields.text('Description', translate=True),

'partner_id': fields.many2one(

'res.partner',

'partner',

help='Select a partner for this brand if it exists.',

ondelete='restrict'),

'logo': fields.binary('Logo File'),

}

class product_template(orm.Model):

_inherit = 'product.template'

# or should it be placed here ?

_columns = {

'product_brand_id': fields.many2one(

'product.brand',

'Brand', help='Select a brand for this product.', ondelete='restrict'),

}

아바타
취소
베스트 답변

May be this will useful to you

_sql_constraints = [

('brand_name_uniq', 'unique(field name)', 'Message you want to display')

]

And define after column definition.

Thanks.

Shamji 

아바타
취소
작성자

Hi Solanki, so would you help me with this problem please ? Just bare in mind i need it for oldAPI. Already know how it works with new one but all my modules were built in old :( and I don't want rebuild whole system yet. Maybe in the future but I'm not ready to use new API :(

작성자

Hi Solanki, so would you help me with this problem please ? Just bare in mind i need it for oldAPI. Already know how it works with new one but all my modules were built in old :( and I don't want rebuild whole system yet. Maybe in the future but I'm not ready to use new API :(

베스트 답변

I checked the word commented by Pawan "so first delete any duplicate named records" and tried to apply the constraint after deleting all the records in the table with no success, duplicates are still there. Finally I discovered that I have to DROP the table from the database and reinstall the module again so the constraint can go into the table definition. I added this reply for clarification

아바타
취소
베스트 답변

Hi Robert,

you can create a sql constraint just using unique(name) as:

_sql_constraints = [('name_uniq', 'unique(name)', 'Brand name must be unique!')]

And keep in mind that there should be no duplicate name already existing in the particular master(table), if it exists then the sql contraint will not apply on that master, so first delete any duplicate named records, and then try updating database again.

Thanks
Pawan


아바타
취소
작성자

I tried but still getting an error :(

In your Post down, "unique(brand)" ,brand must be a column in your master, but i can see that in your column list.. Please add a column " brand: fields.char(string = 'Brand'), " and then try to restart ur server by updating module in order to update the database Or if you wan to add unique constraint on product_brand_id you can use: _sql_constraints = [('brand_uniq',unique('product_brand_id'),'Brand name must be unique!'),]

작성자 베스트 답변

So it should be like that:


class product_template(orm.Model):

_inherit = 'product.template'

_sql_constraints = [('brand_uniq',unique('brand'),'Brand name must be unique!'),] # is this correct ? 

_columns = {

'product_brand_id': fields.many2one(

'product.brand',

'Brand', help='Select a brand for this product.', ondelete='restrict'),

}

아바타
취소
관련 게시물 답글 화면 활동
2
8월 25
2609
1
7월 25
1015
1
8월 25
1151
0
5월 25
1468
2
4월 25
3620