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.
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.
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
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?
@@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)
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
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.😊
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😊
@@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....!
@@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..👍🏻👍🏻
Thank you , You Help me alottt!!
Happy to help!
Thanks !
Keep supporting..
😀
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.
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.
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
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?
@@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)
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...
useful thanks
Glad it helped
Super channel
Thank you brother...and keep supporting and share my channel too ... :)
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
You might've missed makemigrations and migrate
awesome
Thanks a lot... :)
how to add a phone number by the user not at admin in one to one relationship?
U can create a Model for Phone and then link it to User model using OneToOneField...
How can I add a phone number to an user from the shell?
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.😊
if i wanted to full row of user that save in (extra_information)->Table. then how to get full dictionary.!
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😊
@@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....!
@@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..👍🏻👍🏻
@@CodeBand thanks....!
@@rahulmullick7316 Keep going👍🏻
sir what is request.user
docs.djangoproject.com/en/4.1/topics/auth/default/#authentication-in-web-requests