Django Relationships | One to One Relationship | Explained with Example by Code Band

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ส.ค. 2024

ความคิดเห็น • 30

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

    Thank you , You Help me alottt!!

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

      Happy to help!

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

    Thanks !

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

      Keep supporting..
      😀

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

    brother you are printing on cmd but actual website on server is giving the error. why you didnt adress that thing. This is wrong man. You wasted my time but still i gottaa know that we can prinnt those on cmd. thanks for that.

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

      Sorry for the late reply...
      Brother, I was demonstrating how 1-1 rels work in Django. I believe printing in console makes much more sense that wiring up a template or returning an HttpResponse. At the end, it gives you a good idea on 1-1, which you can wire it up to any level you want.

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

    Thank you very much. I just learnt to obtain fields from a one-to-one relationship.
    I thought to ask; how do you activate a relationship with another. For instance, I have a model for creating/ergistering smart meters.
    I also have another model to register users of these smart meters. Now, each user can only have one smart meter and one smart meter can only belong to one person (one to one)
    But by default, the smart meters status (is-active) is false until they are assigned.
    In my user model, I have a field for the meter_id (where i declared the one to one relationship). Now how do i change the state of the meter from is_active = False, to is_active=True, after assigning them

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

      Upon creating the SmartMeter instance, change is_active to True, this is what I understood from the question.
      So, for creating a new SmartMeter instance, you might be doing it possibly just after when you create a new user, OR from any other trigger like activate button. Anyhow, you can specify is_active=True while you are creating, like:
      SmartMeter.objects.create(user=user, is_active=True, ...)
      Assuming you have a user field in SmartMeter model.
      Hope this helps...and also a quick suggestion, y can't you make is_active=True, by default?

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

      @@CodeBand hi thanks for your reply... The is_active field in the meter is actually technically "is_assigned" - when it is created, it is not active, but as soon as it is assigned to a user (when a user is being onboarded), then it becomes active. Think of it as "assigned"...
      SmartMeter.objects.create(user=user, is_active=True, ...)
      This line above is going to be in the user model right? i introduced a post_save signal but I am not sure if I got it right... And there is no user field in the Smart meter model because I would need to provide a user while creating a meter (and this is not the case, no user yet)

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

      Fine...you may do that in the same place where you actually create a user and assign the Meter, having a post_save signal is also fine...

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

    useful thanks

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

      Glad it helped

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

    Super channel

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

      Thank you brother...and keep supporting and share my channel too ... :)

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

    doing the same thing but:
    12:28 with "user = User.objects.get(pk=1).phone"
    i got error AttributeError at /
    'User' object has no attribute 'phone'
    but related name is working good

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

      You might've missed makemigrations and migrate

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

    awesome

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

      Thanks a lot... :)

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

    how to add a phone number by the user not at admin in one to one relationship?

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

      U can create a Model for Phone and then link it to User model using OneToOneField...

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

    How can I add a phone number to an user from the shell?

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

      You can start the shell by executing the previous command :
      python manage.py shell
      Also, you can execute the statements in the same way as you would be doing in the code itself.😊

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

    if i wanted to full row of user that save in (extra_information)->Table. then how to get full dictionary.!

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

      Actually, we are accessing the related model ( (extra_information)->Table ), not the field. So, with that related model object, we can access any of the fields there with this object.
      If your question is like getting all of the rows in (extra_information)->Table, then simply say like that Model.objects.all()
      I'm not sure if I answered the exact question you asked. Please let me know if the issue gets resolved
      Thank you😊

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

      @@CodeBand oke...let me clear to u....! User schema/table is contain auth class...
      using this User table create a another table User_Extra_Information that not pre-defined schema in Sqlite3 using model create this User_Extra_Information schema/table in one-to-one relation...now my question is if access this User and Extra_information along with User_Extra_Information schema/table how to do that...? all information....! nd thank u for ur reply....!

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

      @@rahulmullick7316 you can pretty much do that...and thats what I have explained in the video...you can grab the other table info from one table and vice-versa...Hope it helps...sorry for the late reply...just now only I saw your comment..👍🏻👍🏻

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

      @@CodeBand thanks....!

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

      @@rahulmullick7316 Keep going👍🏻

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

    sir what is request.user

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

      docs.djangoproject.com/en/4.1/topics/auth/default/#authentication-in-web-requests