تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
3051 أدوات العرض

I would like to create a field that has several categories of professions (parent elements) and at the end the profession corresponding to that category appears, as in the product category field of the product model.


How can I do this?


الصورة الرمزية
إهمال
الكاتب أفضل إجابة

a

الصورة الرمزية
إهمال
أفضل إجابة

Hello Tiago,

your new module would have many2one field of same model to make parent and child relation same as product.category has parent_id many2one of product.category model.

define _rec_name as new compute field which compute and store hierarchy name.

please have a look following _compute_complete_name method which depends on name and parent_id


class ProductCategory(models.Model):
_name = "product.category"
_description = "Product Category"
_parent_name = "parent_id"
_parent_store = True
_rec_name = 'complete_name'
_order = 'complete_name'

name = fields.Char('Name', index=True, required=True)
complete_name = fields.Char(
'Complete Name', compute='_compute_complete_name', recursive=True,store=True)
parent_id = fields.Many2one('product.category', 'Parent Category', index=True, ondelete='cascade')
parent_path = fields.Char(index=True)
child_id = fields.One2many('product.category', 'parent_id', 'Child Categories')

@api.depends('name', 'parent_id.complete_name')
def _compute_complete_name(self):
for category in self:
if category.parent_id:
category.complete_name = '%s / %s' % (category.parent_id.complete_name, category.name)
else:
category.complete_name = category.name
Let us know if you still need any help on this, 

Thanks,


Thanks & Regards,



CandidRoot Solutions Pvt. Ltd.

Mobile: (+91) 8849036209
Email: info@candidroot.com
Skype: live:candidroot
Web: https://www.candidroot.com
Address: 1229-1230, Iconic Shyamal, Near Shyamal Cross Road, Ahmedabad, Gujarat 380015
الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
3
مايو 25
2476
1
أبريل 25
1878
3
سبتمبر 24
15157
2
فبراير 24
2930
1
يوليو 23
3070