Skip to Content
Menu
This question has been flagged
2 Replies
5895 Views

I dont know what is the most recommended way to display data from a model (A) in the form of a model (B) [Odoo 11].


So far I've used One2many relations, but in this case I cant (or I dont know how to do it).

I have three models:

class School(models.Model):
_name = 'abby.school'
...

class Agreement(models.Model):
_name = 'abby.agreement'
...
agreement_school = fields.Many2one(comodel_name='abby.school', string='School')
...

class Student(models.Model):
_name = 'abby.student'
...
school = fields.Many2one(comodel_name='abby.school', string='School')
...


Graphically: Agreement > School < Student


I want to display a list of Students in the Agreements form.

 
Could I use an One2many relation? Or a SELECT in any place? (I'm a beginner in Odoo)

Thank you in advance!!

Avatar
Discard
Author

--- I want to display all the students (could be one or eleven or zero) who belongs to a school with an agreement ---

Best Answer

If each student will always have one agreement, then you can use One2many field

class Student(models.Model):
_name = 'abby.student'

agreement_id = fields.Many2one('abby.aggrement', 'Agreement')

class Agreement(models.Model):
_name = 'abby.agreement'

student_ids = fields.One2Many('abby.student', 'agreement_id', string="Students")
If the student can have multiple agreements, then you should use the Many2many field:

class Agreement(models.Model):
_name = 'abby.agreement'

student_ids = fields.Many2many('abby.student', string="Students")

Update:

Since you want to display all the students belonging to the school, you should use the auto-compute Many2many field:


class Agreement(...):

student_ids = fields.Many2many('abby.student', compute='_compute_students')

@api.depends('agreement_school')
def _compute_students(self):
        stud_obj = self.env['abby.student']
for agreement in self:
# Search students belongs to the school of the agreement
stud_ids = stud_obj.search([('school', '=', agreement.agreement_school.id)]).ids
agreement.student_ids = [(6, 0, stud_ids)]




Avatar
Discard
Author

Thanks for answering, Sudhir.

I think that solution does not work because there isnt direct relationship between Student and Agreemet. They both point to School:

Agreement > School < Student

Still, with the m2m field, you can select your students in the agreement. Do you want to auto-select all the students belongs to the school of the agreement?

Author

Thanks, Sudhir.

### Still, with the m2m field, you can select your students in the agreement.
### Do you want to auto-select all the students belongs to the school of the agreement?

Yes, I want to display (only display, not add or delete) the students who belong to a school with an agreement.

Using this:
student_ids = fields.Many2many('abby.student', string="Students") (In Agreement)

How can I specify the model School?

Thanks a lot

Please check my updated answer.

Author

Thank you a lot, Sudhir, for beeing so patient.
It's working... almost!
No errors, but list is empty.
So, I've write these "prints":
class Agreement(...):

student_ids = fields.Many2many(compute='_compute_students')

@api.depends('agreement_school')
def _compute_students(self):

stud_obj = self.env['abby.student']
print ("FooAAA ---")
for agreement in self:
print ("FooBBB agreement:")
print (agreement)
# Search students belongs to the school of the agreement
stud_ids = stud_obj.search([('school', '=', agreement.agreement_school.id)])
print ("FooCCC stud_ids")
print (stud_ids)
print ("FooEEE stud_ids[0]")
print (stud_ids[0])
agreement.student_ids = [(6, 0, stud_ids)]

When I go to an agreement, I get:
FooAAA ---
FooBBB agreement:
abby.agreement(10,)
FooCCC stud_ids
abby.student(1369, 905)
FooEEE stud_ids[0]
abby.student(1369,)

The list in trace "FooCCC" is OK. These are the correct ids to display... but they are not loaded in the list... any idea?

Thanks again!!

Please update bellow code:
agreement.student_ids = [(6, 0, stud_ids)]

Replace (stud_ids.ids):
agreement.student_ids = [(6, 0, stud_ids.ids)]

Author

Hi again, and thanks for keep helping!!

Adding "ids", I get this error:
psycopg2.ProgrammingError: relation "_unknown" does not exist
LINE 1: SELECT "_unknown"."id" as "id" FROM "_unknown" WHERE "_unkno...

Looking for that, I have found two posible causes/solutions. The first one recommends to restart both odoo and postgresql. I did it, but the error continues.
In the second comment, they talk about define relationships between models, but in my case, both models are defined in the same module.

Do you know what the problem could be?
Thanks in advance again!

Sorry, I missed to pass on the object name in the m2m field:

student_ids = fields.Many2many('abby.student', compute='_compute_students')

Author

It is working.
Sudhir, thank you very very much for your time and sharing your knowledge.

Best Answer

Hi,

To get the data from modelA to modelB we can use Many2one relation here.For example you need to get the list of students list in agreement form,

ie,

class Agreement(models.Model):
_name = 'abby.agreement'
student_list_id = fields.Many2one('abby.student', string="Student list")

Regards

Avatar
Discard
Author

Thanks for your answer.

But, if I use a Many2one, I will have a list for choose a student...

I want to display all the students (could be one or eleven or zero) who belongs to a school with an agreement.

Regards