Relational, Computed, Related, Many2one, One2Many, Many2many Fields, Odoo 17: Days6: #1 #1million — Transcript
Full transcript
- 0:01Hello, welcome to my YouTube channel.
- 0:03So, in today's video, we are going to
- 0:06look at relational fields. Relational
- 0:08fields are basically your Many2one,
- 0:11One2many, and Many2many. Okay? How we
- 0:14can add them in the form. Okay? And we
- 0:16have to show them in the tree view.
- 0:18Okay? Relational fields, and we also
- 0:21know how to add computed fields in the
- 0:24form view. Okay? And related fields. So
- 0:27today, we will see how to add computed
- 0:29fields. Related fields and relational
- 0:32fields. Okay? Basically, first,
- 0:35understand what exactly these
- 0:37relational fields are. Right? For
- 0:40example, there is a class. Right? A
- 0:43teacher comes from outside and says, "I
- 0:46need one student." So, he only needs
- 0:50one student for some work. So, he is
- 0:52picking just one student. So that means
- 0:55Many2one. Meaning, he is picking one
- 0:57record. He is selecting one record. He
- 0:59is selecting one student. So it is
- 1:01called a Many2one relational field.
- 1:04Okay. Next one comes One2many. So in
- 1:07One2many, what happens is, like there
- 1:10is a student, he picked one student.
- 1:14Then all the details of that student,
- 1:16like the student's name, student's
- 1:18father's name, student's class, info
- 1:20about the subjects, study in the class.
- 1:23Okay? Everything about the student.
- 1:25Only a single person. Right? To show
- 1:28complete information about one product,
- 1:30we have to add a One2many field. Okay?
- 1:33Right? So in this way, we use the
- 1:35One2many field. And Many2many is when
- 1:38we need multiple records together. We
- 1:42need multiple records together. To
- 1:44select them all at once and show them
- 1:47together, we use the Many2many field.
- 1:49Right? And in One2many, you show all
- 1:52the details of one product. But in
- 1:55Many2many, you can only show their
- 1:57names, in a way. But there, for example
- 2:00, you selected five records. Picked
- 2:03five products from the system. Right?
- 2:05So, the names of those five products
- 2:06will be visible. But their full details
- 2:08won't be visible, like what type of
- 2:10product it is, what the cost is, or the
- 2:12sales. Right? Like for a student, what
- 2:15is their name, address, what do they
- 2:17study, which class are they in? So all
- 2:19those things will not be visible. There
- 2:20, you will only see selected students
- 2:22or selected product records. And in
- 2:24Many-to-Many, you can see all this
- 2:26information. Sorry, in a One-to-Many
- 2:28field, you can see all this information
- 2:30. So, this is a difference between them
- 2:33. Okay? Also, what is a computed field?
- 2:37It's based on a field when we save a
- 2:39form. Right? Based on saving the form,
- 2:42you need to perform some calculations
- 2:44in a field. Like, you need to calculate
- 2:47age or show the sum of two numbers in a
- 2:50third field. Right? This is your
- 2:52computed field. What is a related field
- 2:54? As you might have seen while filling
- 2:58out a form on a website, you write your
- 3:01name here, and below it, there is a
- 3:03confirmation field, another column
- 3:05where you enter your name again, and in
- 3:08a third place, it automatically shows
- 3:11the name you entered. Actually, what
- 3:14happens there is that it is related to
- 3:16another field. Meaning, you enter a
- 3:17value in one field, and it
- 3:18automatically appears in the other.
- 3:20Okay. So, let's start with our code.
- 3:24Let's go. I will close all my tabs for
- 3:28now. Go to the custom and run the
- 3:39server. Server run. It's my localhost.
- 3:58Invalid manifest abs model. Wait, in
- 4:12the custom module, remove this. Working
- 4:24. Okay, so our module was 'Student
- 4:27Management', which we were working on.
- 4:32Right, so today we will add fields to
- 4:34the relations in it. So now, what I
- 4:37want is that here, like for the student
- 4:40, I should create a subject object
- 4:44relation. Subject, like our subject, we
- 4:47create subjects here. So I want to show
- 4:49these same subjects here in the form,
- 4:51that hey, this student studies these
- 4:53many subjects. All those...sorry, all
- 4:56those subjects can be selected here.
- 5:00Right? For this student, for example,
- 5:02this is the student. They study these
- 5:04subjects, or right? So all of them will
- 5:08show here. Now let's go to the student.
- 5:20Here, One-to-Many. Let's do one thing.
- 5:30Before One-to-Many, let's add
- 5:31Many-to-One. Okay, here we will select
- 5:35the class first. Sorry, we will add '
- 5:44class_id' in it and 'Many-to-One' will
- 5:48go here. So in this, we have to provide
- 5:51the first parameter, which is which
- 5:53object to relate with, meaning which
- 5:55table you need to relate it to. I need
- 5:58to create a relation with this table '
- 6:00subject.subject', so I will copy this,
- 6:03put it here, what will be its string,
- 6:05and I will put 'class' here. Okay, I
- 6:08will add this field to the form view as
- 6:12well. Okay? This is my tree view. So I
- 6:16will put it here after the user ID.
- 6:23Name and paste here. Okay. I reload the
- 6:28page and upgrade the model. I copied
- 6:32the model name from the backend and
- 6:35pasted it here. Then run. Now I go here
- 6:40and refresh. So my field will appear
- 6:43here. Sorry, it appeared here. Okay? So
- 6:45as you can see here, as soon as I
- 6:47select it here, you have selected only
- 6:50Hindi here. Okay? And I saved it. I
- 6:53have only selected Hindi here. If I go
- 6:55and look at the record subject records
- 6:58here, there is another field called
- 7:00subject ID. I am not able to see this
- 7:02field there. Now, as I put Hindi here,
- 7:06you can only select one record out of
- 7:08multiple records here. This is called
- 7:12Many2one. Now we will add One2many.
- 7:18Before One2many, let's add. Let's add
- 7:21Many2many. Okay? We will add Many2many
- 7:24inside the class. Okay? This same field
- 7:27we will make Many2many. After this, we
- 7:29will add subjects. We will create a
- 7:32field named subjects inside this for
- 7:34Many2many. Okay? So here we will name
- 7:41it subjects_ids. Okay? Fields, this
- 7:47field of mine will be of Many2many type
- 7:50. It will be Many2many. It is the same
- 7:52thing in this as well. You have to give
- 7:56a relation and then the string. I will
- 8:00go and add this field to my sub-class.
- 8:07I will add it after the teacher ID.
- 8:14Okay? I put it here, my field name was
- 8:18subject_ids and I have to give a widget
- 8:21here too. This is a widget where we
- 8:25have to provide the widget '
- 8:29many2many_tags'. What this does is that
- 8:32those attributes appear in it. Okay?
- 8:35Now you see for yourself what it looks
- 8:38like. Now I refresh here. So as I
- 8:42select here. Like I selected Hindi. A
- 8:44tag-like thing appears. Okay? Here you
- 8:47can select multiple, like in 10th grade
- 8:50, three subjects are taught. You can
- 8:53define it in this way here. And here
- 8:56you will create a class, like if you
- 8:59created 12th, you can select how many
- 9:01subjects can be taught in 12th grade,
- 9:04and you can create subjects over here.
- 9:07So, this is your Many-to-Many. I will
- 9:09rename this now. You can change the
- 9:11string from here too. Or else, you can
- 9:13change it from Python. I will make this
- 9:17"Subject" into "Subjects". So that it
- 9:19looks better that there are multiple
- 9:22subjects in "Subjects". Okay, I have
- 9:25made it "Subjects". Now we go to Akash.
- 9:29So, what did we see just now?
- 9:31Many-to-One and Many-to-Many. And
- 9:33lastly, here is One-to-Many. The method
- 9:36for One-to-Many is a bit different. To
- 9:38add a One-to-Many field, we will have
- 9:40to do something a bit different. Okay.
- 9:45What will we have to do? First, we will
- 9:47have to create a new class here. For
- 9:50One-to-Many. Let's do one thing in the
- 9:55class. Let's show that itself. Okay.
- 10:03Class is fine. Let's just create the
- 10:05class. So it will be clearly understood
- 10:07. We will create the class here. I will
- 10:11define the class here. I will name it
- 10:16something like Subject Line. Subject
- 10:21Line, created its object, put the model
- 10:26in it. Subject Line, so what will be in
- 10:30this? One will be mine. Okay? Here, one
- 10:35will be my subject. Subject, which will
- 10:39be Many-to-One. It will be Many-to-One.
- 10:49Its relation will be with
- 10:51subject.subject. Subject.subject, its
- 10:59string will be. Subject. Okay. This is
- 11:14my subject, okay? And we have to give
- 11:17one more thing here. We can call this
- 11:20an inverse field, or okay, we call it
- 11:22an inverse field or a linking field. It
- 11:25is a connection field, basically, with
- 11:26which field do we have to make the
- 11:28connection? With this object, because
- 11:31we are going to add One-to-Many here,
- 11:32actually we are going to add it inside
- 11:34this object. So for that, we will have
- 11:38to create a connection field inside
- 11:40this object. We will have to create an
- 11:42inverse field. Student, by doing that.
- 11:46I will create it by ID. This is always
- 11:49Many-to-One. Okay? Our connection field
- 11:52, sorry, is Many-to-One. Which is used
- 11:55in One-to-Many. Okay? And here we will
- 11:58give the name of this object. And here
- 12:00we will give this to it. Student Okay?
- 12:08So I have a subject here and this here.
- 12:11Okay? So now I What I do is? I grant
- 12:20access to this sub-object in my
- 12:22security file. Go first so that there
- 12:25are no errors. Give it maximum in
- 12:33security. I have granted access in the
- 12:42security file. Now let's go to the
- 12:44student form. After the student form,
- 12:46if we look here, there is a notebook
- 12:49tag. Okay? There is a notebook tag.
- 12:52Inside this notebook, we start a page.
- 12:55Okay? A page starts inside this. We can
- 12:57give a string to this page here. A
- 13:01string like, well, subject subject.
- 13:05Okay? Let's call it subject details.
- 13:09Subject details. Okay? Inside this, we
- 13:14also need to provide our one-to-many
- 13:18field. Okay? Let's do this much first.
- 13:22After this, we go to student. Now we
- 13:26add a one-to-many relation to our
- 13:28subject form. So here we will do
- 13:33subject line. Okay? In that, we will
- 13:36add a line, in a way, make it line ID.
- 13:39And here field dot one-to-many, inside
- 13:42this we have to give one object. We
- 13:45will create a relation with this. The
- 13:48second is the universal one we created
- 13:50in the name of student. We created it
- 13:53in the name of student ID. Then after
- 13:55this, we give a string. Here we have to
- 14:00provide the string subject details.
- 14:06Okay? This field that I have created,
- 14:08many-to-one, we have created it inside
- 14:11this object, so that all the fields I
- 14:13have inside this object are these. So
- 14:16that I can access them there. Okay? So
- 14:20now I will add its fields there. These
- 14:25are my fields. I will enter the name
- 14:29here. Entered field. Inside this, I
- 14:32started the tree view tag. Inside this,
- 14:35I will put my field. Name and here I
- 14:39will put subject ID and I closed it.
- 14:45Once this is done, understand that your
- 14:47one-to-many has been added. Look,
- 14:52subject details have appeared here now.
- 14:54So now you can choose from here. Look,
- 14:57a popup has appeared here now. Okay?
- 14:59You can go here and save and close it.
- 15:02You will enter the subject here, Hindi,
- 15:04and what is its student ID? One, and I
- 15:06will close it, save. Now what I want is
- 15:09that when I click on this, a popup
- 15:10appears. This popup is asking me to
- 15:13select here, but I don't want it to
- 15:15keep adding as soon as I click. So, for
- 15:18this, there is an attribute. For this,
- 15:21we need to add 'editable top'. There is
- 15:25top, there is bottom; it depends on
- 15:28where you want to edit from. You can
- 15:31also set it to bottom. So, let's see
- 15:33what happens with this. As I click on
- 15:37it, the popup didn't appear for me now.
- 15:41I will click here and can select from
- 15:42here. Alright? So, this is how we can
- 15:46add one-to-many fields. Currently, it
- 15:48is showing only one field here. And if
- 15:50we look into our subject, there are two
- 15:53fields. Right? There is also a subject
- 15:55ID. So, now we will also show the
- 15:57subject ID here. Right? Let's show the
- 15:59subject ID. We will go inside this. So
- 16:05now, you see, the related field will
- 16:07also be used here. Our field was about
- 16:09how the related field will be used? So,
- 16:12we will add the related field here as
- 16:13well. Subject ID, let me see what the
- 16:18name of that field is. Let me go and
- 16:22open one more tab here. I'll go there.
- 16:26What is this field? Developer mode is
- 16:31turned on. Subject ID, Subject, okay.
- 16:40Character type, okay. I have already
- 16:44used subject name/ID above, so I will
- 16:47name this one. Subject code, let's name
- 16:51it code, okay. Code, okay, fine. Fields
- 16:55dot, we will make it of character type
- 16:57because it is a character type there,
- 17:00and for this, there is an attribute
- 17:02called 'related', okay? Inside this, we
- 17:06need to start a string, so which field
- 17:08do we want to make it related to? We
- 17:10want to make it for subject ID. Now I
- 17:12have entered inside this. It means I
- 17:15have come inside this object. Inside
- 17:17this object, there is a field, the same
- 17:19subject ID. So here, I will put a dot
- 17:23and write subject dot subject ID. And I
- 17:28will add a string here. Subject Let's
- 17:35make it subject code. Okay? I made it
- 17:41subject code and I will also show the
- 17:44field here. After this After this, I
- 17:52showed it here. And now I have
- 17:55restarted it and I will refresh it over
- 17:58here. Let's see what happens. So you
- 18:01see, the value came automatically. If I
- 18:03do what here? Look, I change this. I'll
- 18:06select English and the English code
- 18:08will come here automatically. It came,
- 18:09the English code has even appeared. Now
- 18:11, if I select Math here, the Math code
- 18:13will appear automatically. So, this is
- 18:16the function of our related field. Okay
- 18:19? That's how we use the related field.
- 18:22Now comes our computed field. How do we
- 18:25use computed fields? We can use
- 18:29computed fields to count how many
- 18:30records are selected here. We have
- 18:33selected three records. So, I want to
- 18:35show the number of subjects counted
- 18:38here. Okay? I will add one more field
- 18:41here, a subject count field. Okay? In
- 18:44that field, I will ensure that for
- 18:46however many subjects are selected, I
- 18:48display the count of records here. Like
- 18:50here there are three, so the count
- 18:51there will be three. Okay? So, let's
- 18:54add it to our object this way. In the
- 18:58student model, let's call it
- 19:00total_subject. Total subject, and then
- 19:12we set it to integer type. Okay? We
- 19:18give it a string "Subject" and it has
- 19:21an attribute called compute. Okay? This
- 19:24is an attribute. Inside this, there is
- 19:27a string, and we write here that the
- 19:30total subjects should appear. Okay? We
- 19:33provide a function inside this.
- 19:35Basically, we create a function and
- 19:38assign its name here. Okay? So look, it
- 19:44automatically showed me that you can do
- 19:47it like this in self.total_subject,
- 19:50where whatever the length is, that
- 19:53count will appear here. So let's see,
- 19:57first let's add this field here. Going
- 20:00into student, we add it—not here,
- 20:04let's add it after class_id. And I will
- 20:12make it invisible. We have an attribute
- 20:15for that, invisible. Okay? Invisible
- 20:19means what you see right now will
- 20:21disappear. It won't be visible. Okay?
- 20:24So look, as soon as I refreshed, it
- 20:26automatically showed three here. If I
- 20:28add one more here. Look, I added one
- 20:31more subject here, and as soon as I
- 20:34save it, it will become four. See, it
- 20:36became four automatically. This is how
- 20:39you can create a computed field. Okay.
- 20:43So, today we saw how to add relational
- 20:47fields. How we can use computed fields
- 20:51and how to use related fields. Okay? If
- 20:55you have any queries or issues, you can
- 20:58comment on the video. Okay? So thank
- 21:01you.
About this transcript
This page contains the full transcript of Relational, Computed, Related, Many2one, One2Many, Many2many Fields, Odoo 17: Days6: #1 #1million by Odoo Tech , generated from the public captions YouTube serves with the video. The transcript has 2,665 words across 377 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.