Django Custom User Model | Extend User Model Fields | AbstractUser | Django ORM Mastery

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 26

  • @KingdomIncreasingMiracles
    @KingdomIncreasingMiracles Год назад +3

    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
      @veryacademy  Год назад

      Nice to hear from you, hope you are getting on well with your project!

    • @KingdomIncreasingMiracles
      @KingdomIncreasingMiracles Год назад +1

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

  • @diegoc.1212
    @diegoc.1212 Год назад

    Thank you, you won't imagine how long I have been reading trying to understand this :)

  • @RahulGupta-pw5ei
    @RahulGupta-pw5ei Год назад +1

    Great sir i love ur teaching style

  • @waseem1162
    @waseem1162 2 года назад

    Appreciate your hard work 👍

  • @omarbd5659
    @omarbd5659 2 года назад

    fantastic content, thank you very much for your awesome effort

  • @raphaelhunold6812
    @raphaelhunold6812 Год назад +1

    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 ?

    • @veryacademy
      @veryacademy  Год назад

      Have a look at model meta verbose_name

  • @thezeeshann
    @thezeeshann 2 года назад +1

    Thanks man 🙏

  • @shamimanowar1726
    @shamimanowar1726 2 года назад

    Great sir❣️

  • @joy_sam_raj
    @joy_sam_raj 2 года назад

    Thank you very Much

  • @rangabharath4253
    @rangabharath4253 2 года назад

    awesome

  • @enzolarosa4148
    @enzolarosa4148 2 года назад

    Hi thanks a lot for you videos they are very helpful, can you please insert the link to the GitHub Code Repository, thanks

  • @rishabhsharma667
    @rishabhsharma667 2 года назад

    Sir please make videos on microservices.

  • @hasibullahwardak4514
    @hasibullahwardak4514 11 месяцев назад

    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.

  • @AbdullahKhan-ft5bj
    @AbdullahKhan-ft5bj Год назад +1

    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

    • @veryacademy
      @veryacademy  Год назад

      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.

    • @AbdullahKhan-ft5bj
      @AbdullahKhan-ft5bj Год назад

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

  • @georgesmith3022
    @georgesmith3022 2 года назад +1

    you didn't touch the most common requirement of creating a custom user. this is to change authentication with email instead of username

    • @veryacademy
      @veryacademy  2 года назад +1

      Thanks George, there are a few more tutorials to go yet with this.

  • @joy_sam_raj
    @joy_sam_raj 2 года назад

    Please update this in a Github Repo

  • @מאורמאיר-ב9ט
    @מאורמאיר-ב9ט Год назад

    thanks

  • @waseem1162
    @waseem1162 2 года назад

    Appreciate your hard work 👍