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

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ค. 2020
  • 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 ปีที่แล้ว +4

    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 ปีที่แล้ว +1

      Great to hear!

  • @rvft
    @rvft 2 ปีที่แล้ว +5

    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 ปีที่แล้ว +2

      Very glad to hear that.. Thank you :)

  • @ismailhamzah5024
    @ismailhamzah5024 3 ปีที่แล้ว +2

    Tq man! Your the man.. your explanation so clear. Get me to boost my startup project.

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว

      Glad to help! Keep watching😊

  • @user-tt4ug1qs8p
    @user-tt4ug1qs8p ปีที่แล้ว +1

    i loved it very much! such an extended explanation! thx

    • @CodeBand
      @CodeBand  ปีที่แล้ว +1

      Glad it was helpful!

  • @maxwellamoakoagyapong9486
    @maxwellamoakoagyapong9486 2 ปีที่แล้ว +2

    This guy is great, how come I just discovered this channel. Great video!

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Thank you and Welcome aboard!

  • @ches_ter6842
    @ches_ter6842 11 หลายเดือนก่อน

    Thanks for tutorial. Django is so powerfull. Excelent job!

  • @ratulr
    @ratulr 3 ปีที่แล้ว +3

    This video explains model relations very well. Great job

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Very glad to hear that... 😊

  • @thalmathandie9293
    @thalmathandie9293 11 หลายเดือนก่อน

    just wonderful

  • @franciscrawford6208
    @franciscrawford6208 3 ปีที่แล้ว +4

    Very awesome Job bro well explained and to the point love it keep it up

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +2

      Glad you liked it❤️ keep going👍🏻

  • @YouTubeSucksSometime
    @YouTubeSucksSometime 6 หลายเดือนก่อน

    Thank you for this video.

  • @codevalley9511
    @codevalley9511 2 ปีที่แล้ว +1

    Thanks bro for easy clear cut explaination.

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Welcome brother😊

  • @novascotia2015
    @novascotia2015 11 หลายเดือนก่อน

    powli sanam bro. nalla accent.

  • @oruchkin
    @oruchkin 2 ปีที่แล้ว +1

    Very good thanks!

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Glad you liked it!

  • @2minutescoding460
    @2minutescoding460 2 ปีที่แล้ว +1

    bro god blessed you always ,just because of you i solved my error thanx

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +2

      Happy to help

  • @sibisidharth4933
    @sibisidharth4933 2 ปีที่แล้ว +1

    very clear !

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Thanks a lot😊

  • @visco912
    @visco912 3 ปีที่แล้ว +1

    the best tutorial

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Very glad to hear that😊😊

  • @mdshabbirrr
    @mdshabbirrr 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

      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 ปีที่แล้ว +1

      @@CodeBand Done watching those.. awesome content

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +2

      @@mdshabbirrr Thanks a lot and keep supporting❤️

  • @harishramu5458
    @harishramu5458 4 ปีที่แล้ว +1

    Nice one bro...

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Thanks ✌️

  • @mfsdrawings8600
    @mfsdrawings8600 4 ปีที่แล้ว +2

    Good

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Thanks

  • @f.christaintanghanwaye3113
    @f.christaintanghanwaye3113 3 ปีที่แล้ว +1

    good job bro

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Thanks a lot❤️

  • @juanulis3s
    @juanulis3s 3 ปีที่แล้ว +2

    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 ปีที่แล้ว +2

      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 %}

  • @marissavictorio8979
    @marissavictorio8979 3 ปีที่แล้ว +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 ปีที่แล้ว +2

      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 ปีที่แล้ว +1

      @@CodeBand Thanks!!!!!

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      @@marissavictorio8979 Keep going👍🏻

  • @VijayKumar-dq3ph
    @VijayKumar-dq3ph 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      Sorry for the late reply....
      That sounds interesting... have you tried any approach ?

  • @AsadUllah-jh4jg
    @AsadUllah-jh4jg ปีที่แล้ว +1

    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  ปีที่แล้ว +1

      Thanks :)
      You can get the department object by referencing from employee object.

  • @sivaramk4849
    @sivaramk4849 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      Sorry for the late reply...
      Is this fixed ?

  • @admfotad
    @admfotad 3 ปีที่แล้ว +1

    How would you query every Department with their corresponding (all) Emloyyes ?

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@admfotad Keep going ❤️

  • @mayanktripathi4u
    @mayanktripathi4u 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

      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 ปีที่แล้ว +2

      @@CodeBand Thanks this works.

  • @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

  • @saitejaguntupalli9085
    @saitejaguntupalli9085 7 หลายเดือนก่อน

    How to display in temples

  • @SamOween
    @SamOween 3 ปีที่แล้ว +1

    How could we make the employee be in more than one department? Please help!

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

    I am using allauth for user authentications... any idea how to use the users as a foreign key?

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      That's something you've to figure out by looking into allauth docs...

    • @swagataroy6694
      @swagataroy6694 3 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      Glad to hear that...keep going😊

  • @vonputtkamer1673
    @vonputtkamer1673 3 ปีที่แล้ว +1

    please for the source code link of this project sir its AWESOME

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +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.😊

  • @ACIDGameReviews
    @ACIDGameReviews 3 ปีที่แล้ว +1

    I wanna link the Many To Many to my Front-End with Ajax and JQuery, how would I do this?

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      I didn't get you well...As far as AJAX and stuff like that, you can absolutely do that... :)

    • @ACIDGameReviews
      @ACIDGameReviews 3 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@CodeBand hopefully couldn't really find anything though

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      @@ACIDGameReviews 😮

  • @misfarsiddeek3105
    @misfarsiddeek3105 4 ปีที่แล้ว +1

    How to write where clause in orm? And how to join the table?

    • @misfarsiddeek3105
      @misfarsiddeek3105 4 ปีที่แล้ว +1

      @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 ปีที่แล้ว +1

      @Vasu deva Thanks for the reply brother...

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      @@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 ปีที่แล้ว +1

      @Vasu deva ohhh.. Now I can understand little by little. Thnx alot.

    • @misfarsiddeek3105
      @misfarsiddeek3105 4 ปีที่แล้ว +1

      @@CodeBand sure. I will check this. I want to do development using django.

  • @amongdoomers9464
    @amongdoomers9464 4 ปีที่แล้ว +2

    Hey dude , Are you Indian?

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      Yeah... :)