Hi Zander, I wanted to say that you have explained the User information better here than anywhere else I have studied. You are an excellent teacher. I love how detailed you are. I am very detailed in how I do anything so this is refreshing. I was confused on this topic regarding User and not sure how to proceed in my K.I.M. project. I certainly do not want my db setup wrong. Thank you very much Zander. You really helped me tremendously with the User tutorials.
@@veryacademy thank you for your support of my mission. It is awesome to hear back from you too! 😊I plan to deploy my new site this week to replace the old one. I still have lots to add but I am adding the functionality I need in detail as I will need it for my work. I didn't want to rush this and have a weak foundation. One thing I have learned since we last spoke, when I started my new website, is that I learn and understand the code best from your teachings since you explain exactly what is going on with the code and the goals of the code used! Thank you truly. Have a great week!
Really enjoy your video. I was wondering why your custom AbstractUser (NewUser) is shown as "Users" in the left panel in the Admin. How can you customize the label of you model name in in the admin ?
I followed up I created superuser and ordinary user accounts. I can login with super user account but in ordinary user account it tells me: wrong username or password while I created many ordinary accounts from admin- superuser can anyone help.
In Django, the request.user object represents the current user that is authenticated. By default, Django's built-in authentication system provides a User object with certain predefined fields like username, email, etc. If you need additional fields for your users, you have a few options to achieve this: Extending the User Model: You can extend the default User model by creating a custom user model that includes additional fields. This allows you to store extra information for your users. Here's an example of how you can do this: # models.py from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): # Add your extra fields here extra_fields = models.CharField(max_length=100, blank=True) Remember to update your settings.py to use your custom user model: # settings.py AUTH_USER_MODEL = 'yourapp.CustomUser' Profile Model: Another common approach is to create a separate UserProfile model associated with the default User model using a one-to-one relationship. This allows you to keep the default User model clean while adding additional fields in the profile model: # models.py from django.contrib.auth.models import User from django.db import models class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) extra_fields = models.CharField(max_length=100, blank=True) User Model Extension with AbstractBaseUser: For more flexibility and customization, you can create a custom user model by inheriting from AbstractBaseUser and create your own user model with the desired fields. After creating or extending your user model, don't forget to run makemigrations and migrate to apply the changes to your database. Once you have extended the user model or created a separate profile model, you should be able to access the extra_fields attribute through the request.user object in your views. For example: # views.py from django.contrib.auth.decorators import login_required from django.shortcuts import render @login_required def my_view(request): # Access the extra_fields attribute of the authenticated user extra_fields_value = request.user.extra_fields # Your view logic here ... return render(request, 'template.html', context) Remember to ensure that the user is authenticated before accessing the extra_fields attribute by using the @login_required decorator or checking request.user.is_authenticated in your view.
Hi Zander, I wanted to say that you have explained the User information better here than anywhere else I have studied. You are an excellent teacher. I love how detailed you are. I am very detailed in how I do anything so this is refreshing. I was confused on this topic regarding User and not sure how to proceed in my K.I.M. project. I certainly do not want my db setup wrong. Thank you very much Zander. You really helped me tremendously with the User tutorials.
Nice to hear from you, hope you are getting on well with your project!
@@veryacademy thank you for your support of my mission. It is awesome to hear back from you too! 😊I plan to deploy my new site this week to replace the old one. I still have lots to add but I am adding the functionality I need in detail as I will need it for my work. I didn't want to rush this and have a weak foundation. One thing I have learned since we last spoke, when I started my new website, is that I learn and understand the code best from your teachings since you explain exactly what is going on with the code and the goals of the code used! Thank you truly. Have a great week!
Thank you, you won't imagine how long I have been reading trying to understand this :)
Great sir i love ur teaching style
So nice of you
Appreciate your hard work 👍
fantastic content, thank you very much for your awesome effort
Really enjoy your video. I was wondering why your custom AbstractUser (NewUser) is shown as "Users" in the left panel in the Admin. How can you customize the label of you model name in in the admin ?
Have a look at model meta verbose_name
Thanks man 🙏
Great sir❣️
Thank you very Much
awesome
Hi thanks a lot for you videos they are very helpful, can you please insert the link to the GitHub Code Repository, thanks
Sir please make videos on microservices.
I followed up
I created superuser and ordinary user accounts.
I can login with super user account
but in ordinary user account it tells me:
wrong username or password
while I created many ordinary accounts from admin- superuser
can anyone help.
hi guys can anyone help me I am unable to refer to the extra_fields using request.user.extra_fields in views, how can I achieve this
In Django, the request.user object represents the current user that is authenticated. By default, Django's built-in authentication system provides a User object with certain predefined fields like username, email, etc. If you need additional fields for your users, you have a few options to achieve this:
Extending the User Model: You can extend the default User model by creating a custom user model that includes additional fields. This allows you to store extra information for your users. Here's an example of how you can do this:
# models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
# Add your extra fields here
extra_fields = models.CharField(max_length=100, blank=True)
Remember to update your settings.py to use your custom user model:
# settings.py
AUTH_USER_MODEL = 'yourapp.CustomUser'
Profile Model: Another common approach is to create a separate UserProfile model associated with the default User model using a one-to-one relationship. This allows you to keep the default User model clean while adding additional fields in the profile model:
# models.py
from django.contrib.auth.models import User
from django.db import models
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
extra_fields = models.CharField(max_length=100, blank=True)
User Model Extension with AbstractBaseUser: For more flexibility and customization, you can create a custom user model by inheriting from AbstractBaseUser and create your own user model with the desired fields.
After creating or extending your user model, don't forget to run makemigrations and migrate to apply the changes to your database.
Once you have extended the user model or created a separate profile model, you should be able to access the extra_fields attribute through the request.user object in your views. For example:
# views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
@login_required
def my_view(request):
# Access the extra_fields attribute of the authenticated user
extra_fields_value = request.user.extra_fields
# Your view logic here
...
return render(request, 'template.html', context)
Remember to ensure that the user is authenticated before accessing the extra_fields attribute by using the @login_required decorator or checking request.user.is_authenticated in your view.
@@veryacademy thank for the reply i followed the same steps as you mentioned but it still shows extra_fields is not a defined fields in class User
you didn't touch the most common requirement of creating a custom user. this is to change authentication with email instead of username
Thanks George, there are a few more tutorials to go yet with this.
Please update this in a Github Repo
What is wrong?
thanks
Appreciate your hard work 👍