Django Relationships | One to Many Relationship | Many to One Relationship | Foreign Key | Example

  Рет қаралды 28,604

Code Band

Code Band

4 жыл бұрын

In this video, I'm gonna be explaining about one to many relationship in Django and many to one relationship in Django. At first, we will show you pictorial whiteboard representation of what exactly is Django one to many relationship and Django many to one relationship. We will then make a practical example of creating two models and connecting or making a relationship between them using Django Model ForeignKey ( foreign key ) field. We will discuss about fetching related model data and also the inverse relationships. We will show you how to override the default reverse model manager from model_set to any name. We will show you how to access related model data using field lookups in django. This is the second video of Django relationships playlist where we will be discussing about many to one relationship in django, one to many relationship in django, many to one relationship in django, many to many relationship in django, foreign key in django and much more content.
Subscribe | Code Band
Full Playlist : Django Relationships | Explained with Example by Code Band : • Django Relationships |...
Other videos in Code Band:
Django Authentication | The easiest and actual way of doing it : • Django Authentication ...
Custom Model Manager & Model QuerySet in Django : • Custom Model Manager &...
Render Django Form Manually : Make Custom Form with Form Validations - • Render Django Form Man...
Generic Class Based Views in Django | Introduction & Create View in Django | Learn by Example | CRUD : • Generic Class Based Vi...
How to Deploy Django Project on Heroku for Free : • How to Deploy Django P...
Connect Django with MySQL : • How to Connect Django ...
Instagram Automation using Python : • Instagram Automation u...
Top 5 Programming Languages to Learn in 2020 : • Top 5 Programming Lang...
:) You Can Find Me On:
My Website - jasim.tech/
Linkedin - / akjasim
Github - github.com/akjasim/
Facebook - / jasimakjasim
Twitter - / akjasim
Instagram - / jasim_ak

Пікірлер: 88
@jingai5548
@jingai5548 2 жыл бұрын
I have been confused about this for a couple of months. This is the best explanation I have seen so far. Very clear and easy to follow. Thank you!
@CodeBand
@CodeBand 2 жыл бұрын
Great to hear!
@rvft
@rvft 2 жыл бұрын
Literally the best tutorial for OneToMany and ManyToOne relationship tutorial on entire youtube! Thank you so much for this amazing tutorial (BTW I enabled color inversion so video was like you enabled dark mode lol)
@CodeBand
@CodeBand 2 жыл бұрын
Very glad to hear that.. Thank you :)
@ismailhamzah5024
@ismailhamzah5024 3 жыл бұрын
Tq man! Your the man.. your explanation so clear. Get me to boost my startup project.
@CodeBand
@CodeBand 3 жыл бұрын
Glad to help! Keep watching😊
@maxwellamoakoagyapong9486
@maxwellamoakoagyapong9486 2 жыл бұрын
This guy is great, how come I just discovered this channel. Great video!
@CodeBand
@CodeBand 2 жыл бұрын
Thank you and Welcome aboard!
@user-tt4ug1qs8p
@user-tt4ug1qs8p Жыл бұрын
i loved it very much! such an extended explanation! thx
@CodeBand
@CodeBand Жыл бұрын
Glad it was helpful!
@franciscrawford6208
@franciscrawford6208 3 жыл бұрын
Very awesome Job bro well explained and to the point love it keep it up
@CodeBand
@CodeBand 3 жыл бұрын
Glad you liked it❤️ keep going👍🏻
@ches_ter6842
@ches_ter6842 11 ай бұрын
Thanks for tutorial. Django is so powerfull. Excelent job!
@novascotia2015
@novascotia2015 Жыл бұрын
powli sanam bro. nalla accent.
@ratulr
@ratulr 3 жыл бұрын
This video explains model relations very well. Great job
@CodeBand
@CodeBand 3 жыл бұрын
Very glad to hear that... 😊
@YouTubeSucksSometime
@YouTubeSucksSometime 6 ай бұрын
Thank you for this video.
@thalmathandie9293
@thalmathandie9293 Жыл бұрын
just wonderful
@mdshabbirrr
@mdshabbirrr 3 жыл бұрын
Haven't came across anyone explaining relational database query this way.. related_name thing was new to me and it helped me a lot to understand the concepts.. will be looking for more from you regarding django
@CodeBand
@CodeBand 3 жыл бұрын
Very glad to hear that❤️ and there are a lot of videos you might helpful like prefetch_related, select_related etc... in this channel. Keep going😊
@mdshabbirrr
@mdshabbirrr 3 жыл бұрын
@@CodeBand Done watching those.. awesome content
@CodeBand
@CodeBand 3 жыл бұрын
@@mdshabbirrr Thanks a lot and keep supporting❤️
@2minutescoding460
@2minutescoding460 2 жыл бұрын
bro god blessed you always ,just because of you i solved my error thanx
@CodeBand
@CodeBand 2 жыл бұрын
Happy to help
@codevalley9511
@codevalley9511 2 жыл бұрын
Thanks bro for easy clear cut explaination.
@CodeBand
@CodeBand 2 жыл бұрын
Welcome brother😊
@marissavictorio8979
@marissavictorio8979 3 жыл бұрын
Hi, I have 4 models Employee-holds the basic info of employees, Records-holds the records of the Employee with a One to one relationship with the Employee using employee_id as primary key. Also Record models is related to TimeSchedule with emp_time_sched as key (many to one relationship if you look from records). Now, I need to refer to Record model to get the employee_id, and also to get emp_time_sched to get thru to TimeSched Model. And these info will go to DailyTimeRecord Model. The error I am getting is that employee_id is an instance of the Employee. I did not issue any query to Employee model since I can get employee_id from record. Why am I getting such error? Hope you understand what I am after and give me advise on what to do with the error I am getting. Also I copied your code in my machine, and come across the same problem when I issue the: >>>Department.objects.get(name='Accounts').employee_set.all() Attribute error: 'Department' object has no attribute 'employee_set' >>> Emp.objects.create(name='John Simpson', age=35, department.objects.get(name='Accounts')) File "", line 1 SyntaxError: positional argument follows keyword argument These are the errors I encountered with the above models of mine. Thanks.
@CodeBand
@CodeBand 3 жыл бұрын
Department.objects.get(name='Accounts').employee_set.all() Attribute error: 'Department' object has no attribute 'employee_set' Here, your model name might be different..make sure its Employee OR Your Employee model might not have a foreign key to Department model... >>> Emp.objects.create(name='John Simpson', age=35, department.objects.get(name='Accounts')) File "", line 1 SyntaxError: positional argument follows keyword argument Here, it is a syntax error, the third argument you passed is positional whereas the first two arguments are keyword arguments...it's not allowed...positional argument should not be placed after keyword arguments... You can refer Employee model from Record model like below..(assuming name field exists in Employee model) Record.objects.get(pk=1).employee.name Or the other way round, (assuming name field exists in Record model) Employee.objects.get(name='Hi').record.name The above two possibilities assume OneToOneRelationship b/w Employee and Record model... Now, you can access TimeSchedule model from Record model like below Record.objects.get(id=1).emp_time_sched.id Or the other way round, TimeSchedule.objects.get(id=1).record_set.all() (Assuming many-to-one relationship) Hope this helps😊
@marissavictorio8979
@marissavictorio8979 3 жыл бұрын
@@CodeBand Thanks!!!!!
@CodeBand
@CodeBand 3 жыл бұрын
@@marissavictorio8979 Keep going👍🏻
@juanulis3s
@juanulis3s 3 жыл бұрын
Nice video thank you for sharing. Is it possible to get all Departments with all employees in one single query? So that departments could display its employees in an html accordion. I have not found how to do that yet. Thank you.
@CodeBand
@CodeBand 3 жыл бұрын
Yes, you can, with this setup you can do that... {% for department in departments %} {{Title of Accordion}} {% for employee in department.employee_set.all %} {{ employee name }} {% endfor %} {% endfor %}
@VijayKumar-dq3ph
@VijayKumar-dq3ph 2 жыл бұрын
Hi , do you have video explaining foreign key relations among tables of different databases or any hint on how to achieve that?
@CodeBand
@CodeBand 2 жыл бұрын
Sorry for the late reply.... That sounds interesting... have you tried any approach ?
@oruchkin
@oruchkin 2 жыл бұрын
Very good thanks!
@CodeBand
@CodeBand 2 жыл бұрын
Glad you liked it!
@sivaramk4849
@sivaramk4849 2 жыл бұрын
I have a small doubt this all queries are we writing inside views. Py file? I have wrote it but lot of error come to me. Plz give the suggestion
@CodeBand
@CodeBand 2 жыл бұрын
Sorry for the late reply... Is this fixed ?
@shivrajshivu979
@shivrajshivu979 3 жыл бұрын
Hi...i have a doubt..can you please help me. For exmaple i have two tables called countries and universities. Country has many universities and universirty belong to one and only country. So in Country table how should i relate the University and in universitry table how should i relate country . Is there something alternative in django for listfiled in mangodb . Beacuse i thought of using listfiled fuctionality for storing the univerties in the Country table.
@CodeBand
@CodeBand 3 жыл бұрын
Please have a look at embedded models... django-mongodb-engine.readthedocs.io/en/latest/topics/embedded-models.html
@harishramu5458
@harishramu5458 4 жыл бұрын
Nice one bro...
@CodeBand
@CodeBand 4 жыл бұрын
Thanks ✌️
@sibisidharth4933
@sibisidharth4933 2 жыл бұрын
very clear !
@CodeBand
@CodeBand 2 жыл бұрын
Thanks a lot😊
@mayanktripathi4u
@mayanktripathi4u 3 жыл бұрын
Is there a way we could get all the Department and their Employee (irrespective if the Department has an Employee or not).? Please share the queryset for this. may be similar to Department.objects.all().employee_set.all() # this is erroring out.
@CodeBand
@CodeBand 3 жыл бұрын
You can just get all the departments, and in the template, while looping through the departments, you can give an inner loop to get all the employees too... To get all Departments : Department.objects.all() In template : {% for department in departments %} .... {% for employee in department.employee_set.all %} .... {% endfor %} {% endfor %}
@mayanktripathi4u
@mayanktripathi4u 3 жыл бұрын
@@CodeBand Thanks this works.
@AsadUllah-jh4jg
@AsadUllah-jh4jg Жыл бұрын
best video I watched so far. But how to get list of all employees where each employee object contains a sub-object with department details?
@CodeBand
@CodeBand Жыл бұрын
Thanks :) You can get the department object by referencing from employee object.
@visco912
@visco912 3 жыл бұрын
the best tutorial
@CodeBand
@CodeBand 3 жыл бұрын
Very glad to hear that😊😊
@admfotad
@admfotad 4 жыл бұрын
How would you query every Department with their corresponding (all) Emloyyes ?
@CodeBand
@CodeBand 3 жыл бұрын
I have shown that in the video itself, you might have missed. Anyways, departments = Department.objects.all() for dep in departments: print(department.name) for employee in dep.employee_set.all(): print(employee.name) This is the idea..Reverse relationships, please watch the video once again, so that you will get a better understanding😊
@admfotad
@admfotad 3 жыл бұрын
@@CodeBand Thanks man, my english isn't fluent so i could miss a clue - i'm from Poland. Glad that you share your knowledge , really appreciate - regards
@CodeBand
@CodeBand 3 жыл бұрын
@@admfotad Keep going ❤️
@SamOween
@SamOween 3 жыл бұрын
How could we make the employee be in more than one department? Please help!
@CodeBand
@CodeBand 3 жыл бұрын
That's where Many-to-Many relationships comes into play... Read Django Docs here : docs.djangoproject.com/en/3.2/topics/db/examples/many_to_many/
@swagataroy6694
@swagataroy6694 3 жыл бұрын
I am using allauth for user authentications... any idea how to use the users as a foreign key?
@CodeBand
@CodeBand 3 жыл бұрын
That's something you've to figure out by looking into allauth docs...
@swagataroy6694
@swagataroy6694 3 жыл бұрын
@@CodeBand nvm I figured it out... For anyone stuck in my situation you can import SocialAccount from allauth.socialaccount.models... Thanks for replying though... great vid
@CodeBand
@CodeBand 3 жыл бұрын
Glad to hear that...keep going😊
@f.christaintanghanwaye3113
@f.christaintanghanwaye3113 3 жыл бұрын
good job bro
@CodeBand
@CodeBand 3 жыл бұрын
Thanks a lot❤️
@mfsdrawings8600
@mfsdrawings8600 4 жыл бұрын
Good
@CodeBand
@CodeBand 4 жыл бұрын
Thanks
@saitejaguntupalli9085
@saitejaguntupalli9085 7 ай бұрын
How to display in temples
@ACIDGameReviews
@ACIDGameReviews 3 жыл бұрын
I wanna link the Many To Many to my Front-End with Ajax and JQuery, how would I do this?
@CodeBand
@CodeBand 3 жыл бұрын
I didn't get you well...As far as AJAX and stuff like that, you can absolutely do that... :)
@ACIDGameReviews
@ACIDGameReviews 3 жыл бұрын
@@CodeBand are there any tutorials for this? Coz I can do it for the Django admin database but not for the front end
@CodeBand
@CodeBand 3 жыл бұрын
@@ACIDGameReviews I personally haven't noticed any video that explains it clearly..but still you can search..there should be a solution...👍🏻
@ACIDGameReviews
@ACIDGameReviews 3 жыл бұрын
@@CodeBand hopefully couldn't really find anything though
@CodeBand
@CodeBand 3 жыл бұрын
@@ACIDGameReviews 😮
@vonputtkamer1673
@vonputtkamer1673 3 жыл бұрын
please for the source code link of this project sir its AWESOME
@CodeBand
@CodeBand 3 жыл бұрын
Glad to hear that..as for the source code, the video is actually explaining the concept and I think there's no need of source code and that's why I haven't put that up in the description section.😊
@misfarsiddeek3105
@misfarsiddeek3105 4 жыл бұрын
How to write where clause in orm? And how to join the table?
@misfarsiddeek3105
@misfarsiddeek3105 4 жыл бұрын
@Vasu deva ohh. Thnx bro. That mean, can we use more than one filter in same query, for example, if want to use more than one where clause I can use it in normal sql. In the orm also can I use like that?
@CodeBand
@CodeBand 4 жыл бұрын
@Vasu deva Thanks for the reply brother...
@CodeBand
@CodeBand 4 жыл бұрын
@@misfarsiddeek3105 I was little late brother...@Vasu deva did the thing. Now, you could of course use multiple filters in same query which is otherwise known as chaining filters. See more on Docs : docs.djangoproject.com/en/3.0/topics/db/queries/#chaining-filters
@misfarsiddeek3105
@misfarsiddeek3105 4 жыл бұрын
@Vasu deva ohhh.. Now I can understand little by little. Thnx alot.
@misfarsiddeek3105
@misfarsiddeek3105 4 жыл бұрын
@@CodeBand sure. I will check this. I want to do development using django.
@amongdoomers9464
@amongdoomers9464 4 жыл бұрын
Hey dude , Are you Indian?
@CodeBand
@CodeBand 4 жыл бұрын
Yeah... :)
Understanding Django Model Relationships
25:56
Pretty Printed
Рет қаралды 94 М.
Clowns abuse children#Short #Officer Rabbit #angel
00:51
兔子警官
Рет қаралды 72 МЛН
Django Rest Framework API #22 / Many To Many Relationship , Nested Data
31:20
Scenario based on Many-to-many relationship | Power BI Interview
7:04
Learn the Basics of Django Signals
22:37
Matt Freire
Рет қаралды 49 М.
Django ORM | Foreign Key Deletion Constraints
18:53
Very Academy
Рет қаралды 7 М.
Creating Many-To-Many Relationships in Flask-SQLAlchemy
7:28
Pretty Printed
Рет қаралды 43 М.