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

class Student(models.Model):

default_selected_student = fields.Boolean(default=False)

if I select one student, the other student should be unselected.  So, there can be only one who is selected.

How can i do that?



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

use api.constrains

@api.constrains('default_selected_student')
def _change_student_status(self):
    if self.default_selected_student:
        students = self.env['<_name>'].search([('id', '!=', self.id)])
        for student in students:
            student.default_selected_student = False

Ok... I think that you could do the the for with a single write, but don't know the exactly how to do it off my head.

Replace the <_name> with your models value for _name.  



الصورة الرمزية
إهمال

you could probably use api.depends

أفضل إجابة

for me its not working i am getting error plz solve this error brother


class AcademicYear (models.Model):
        _name = 'edudibonmaster.academicyear'
        _rec_name = 'academicyear'
        _description = 'Academic Year'

        academicyear = fields.Char(string="Academic Year", size = 9, required = True)
        startdate = fields.Date(string="Start Date", required = True)
        enddate = fields.Date(string="End Date", required = True)
        current = fields.Boolean(string = "Active", required = True)

        # constraints unique value


        @api.constrains('current')
        def _change_academic_year(self):
                if self.current:
                        years = self.env['edudibonmaster.academicyear'].search([('id', '!=', self.id)])
                        for year in years:
                                year.current = False

Error :

  File "/opt/odoo/odoo/odoo/service/model.py", line 141, in wrapper
    raise ValidationError(inst[0])
TypeError: 'IntegrityError' object does not support indexing


الصورة الرمزية
إهمال
الكاتب

can you send me where the problem is?

ok i will send my module

hi can u help me

الكاتب

try to use @api.multi

الكاتب

I mean @api.onchange('current')

الكاتب أفضل إجابة

it works. thanks a lot.

الصورة الرمزية
إهمال

no problem... than you could like it or select it as the correct answer :)

المنشورات ذات الصلة الردود أدوات العرض النشاط
1
مارس 18
4060
1
يناير 20
4904
0
نوفمبر 18
2771
1
مارس 25
834
2
يوليو 24
2426