Extend Your Django User Framework With a One To One Field

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024
  • Sometimes you want your user model to have more than just an email and a password, so one way to extend the user model is by using a one to one field. In this video, I'll show you how to use it in Django.
    Need one-on-one help with your project? I can help through my coaching program. Learn more here: prettyprinted....
    Join my free course on working with the database and models in Django: prettyprinted....
    Twitter: / pretty_printed
    Github: github.com/pre...

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

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

    Join my free course on working with the database and models in Django: prettyprinted.com/djangodatabase

    • @thomas-sinkala
      @thomas-sinkala 4 ปีที่แล้ว

      Hi Anthony, I want to personally say thank you for your videos. I wish I could hit the subscribe button twice. If you ever got patreon. I will make sure to join.

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

    Thank you so much dude. Your video was the end of a 14 hour slogfest through docs and other videos that didn't work out. Thank you so much. Keep up the good work.

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      I'm glad i could help! Thanks for watching.

  • @jackoripo
    @jackoripo 4 ปีที่แล้ว +9

    Thank you, I have been struggling with this for 6 hours for my final Milestone Project.

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

      +1

    • @winningtech5
      @winningtech5 2 ปีที่แล้ว

      I've been struggling for 2 days trying to add a user_img. It works but when using the usercreationform it doesn't give me the error

  • @posihdun7264
    @posihdun7264 2 ปีที่แล้ว

    started watching this... ended up calling into work:) Awesome video!

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

    Finally I have found the complete tutorial. Thanks man.

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

    Omg, I was looking for the same thing from long time and i was checking your django playlist and noticed that you uploaded this one week ago.
    Thanks a lot for this tutorial.

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      You're welcome! Thanks for watching.

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

    We're currently building a django project in the coding bootcamp I'm in, and I was looking to extend the user in just this way! Found a lot of other resources and documentation that tried to explain how exactly I should implement the OneToOne field, but you explained everything super concisely! Really awesome tutorial! Instant sub :D

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

      I'm glad the video helped! Thanks for watching.

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

    Very helpful video .. I couldn't search the solution for showing the one to one model fields info anywhere .. but u got my back.. Thanks Sir .. for your amazing work .. keep uploading new content to help us with our work

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      I'm glad my video could help you. Thanks for watching.

  • @alensebastian4677
    @alensebastian4677 2 ปีที่แล้ว

    i was looking for a good tutorial which does what its says and finally I found peace!!!
    ,thank you!!

  • @ammarkashif3774
    @ammarkashif3774 4 ปีที่แล้ว

    OMG! You saved my smol life. I got f****ed for straight 3 days and couldn't extend the default user model. THANK YOU SO MUCH!

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

    What I was exactly looking for.
    You are good man...
    Keep it up and thanks.
    You just earned a subscriber.

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

    Finally I found what i'm looking for for 2 days , thank u so much 💜

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

    Great. Thank you. I am busy with python on these days and you are one of the best channel.

  • @alstev7784
    @alstev7784 2 ปีที่แล้ว

    Thank you for your clear and straightforward explanation. It would've been a whole day for me to understand the documentation xD

  • @editorera239
    @editorera239 2 ปีที่แล้ว

    Thanks man it solved my problem
    I was too looking for extending fields beside existing user model offered by django.

  • @Alex-ho8ke
    @Alex-ho8ke 2 ปีที่แล้ว

    Thank you so much dude, from Russia:)

  • @ZainAli-me4qk
    @ZainAli-me4qk 4 ปีที่แล้ว

    You deserve million subscribers

  • @kishantrivedi3216
    @kishantrivedi3216 4 ปีที่แล้ว

    This was clear and to the point. Just perfect

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

    Thanks! This was really helpful!

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

    This is EXACTLY what I was looking for. Thank you!

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

    Great Content !! Thanks for helping

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

    Thanks you bro well done

  • @akshhay
    @akshhay 5 ปีที่แล้ว +4

    This helped me allot, I am really thankful ♥️🙏

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      I'm glad it did. Thanks for watching!

  • @user-le1nf2zf9l
    @user-le1nf2zf9l 3 ปีที่แล้ว

    It's good solution, but there is problem if you need follow sequence fields.
    For example you need place default django's field then your custom field then default field again.
    Do you know solution of descirbed problem?

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

    How to edit profile page by user ?

  • @afekhaijoseph5678
    @afekhaijoseph5678 4 ปีที่แล้ว

    This was awesome. Quick question, what if i wanted to add the other information related to the user like the location and age after they have registered and logged in to the app.

  • @ZainAli-me4qk
    @ZainAli-me4qk 4 ปีที่แล้ว

    Finally found the correct tutorial which i was looking for. Thanks man 👍

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

    How did you get user.user_profile.location at 10:45 ? Where did "user_profile" come from?

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

    finally after 2 hours, thank u

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

    Great! I was looking for it from long time.
    Thank you very much!

  • @k.m.8742
    @k.m.8742 3 ปีที่แล้ว +1

    Thank you for the video! This is what I want to make. But, I have an error when I summit the registration page. The error is UNIQUE constraint failed: userprofile_profile.user_id. Is there any way to solve this error? Thank you

  • @thomas-sinkala
    @thomas-sinkala 4 ปีที่แล้ว

    Any help on authenticating users login request using email & username? Not just username.

  • @ches_ter6842
    @ches_ter6842 2 ปีที่แล้ว

    Hi! How we can get an access to this custom fields if we need edit userprofile? Thanks

  • @patrickmutuku
    @patrickmutuku 5 ปีที่แล้ว

    I must buy that course. Am sure it will fast track my Django learning process.

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      Hope to see you in the course!

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

    just awesome. Thank you so much boss.

  • @gautampramod1734
    @gautampramod1734 4 ปีที่แล้ว

    I was trying to build a project .. i did extend default user model.. i was successful but once i login user from form.. and open admin panel it wil logout the superuser from there.. what i want is the user i created doesnot affect the super user. Plz help me..

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

    Can we have done this with our custom form instead of
    extending UserCreationForm???

  • @TuanNguyen-mw2cx
    @TuanNguyen-mw2cx 2 ปีที่แล้ว

    19/6/2022 perrfect course

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

    how can I implement multiple user types and redirect them different pages on login? I can't find it please help :(:(:((:

    • @tigozigo1234
      @tigozigo1234 4 ปีที่แล้ว

      same i looking for

    • @techyguys4394
      @techyguys4394 4 ปีที่แล้ว

      Looking for same did you find out

    • @tahademirer
      @tahademirer 4 ปีที่แล้ว

      @@techyguys4394 Hi. I tried using flags. There was 2 types of users in my project. It works, redirects different pages. But honestly, its not efficient enough I think. You can find the code here 👉 github.com/tahademirer/Hospital-Appointment-System

  • @AhmedAtef-lj4rq
    @AhmedAtef-lj4rq 3 ปีที่แล้ว

    join() argument must be str, bytes, or os.PathLike object, not 'dict'
    how to solve this

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

    thanks brother!

  • @abderrahmanem9514
    @abderrahmanem9514 4 ปีที่แล้ว

    Thanks !

  • @kaustubhsawant9653
    @kaustubhsawant9653 2 ปีที่แล้ว

    How to add a field of a model to another model.please you tell me the code for it

  • @gabrieledani3394
    @gabrieledani3394 2 ปีที่แล้ว

    Thank you so so very much!

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

    u r truly amazing

  • @arjunbhat8267
    @arjunbhat8267 4 ปีที่แล้ว

    Hey i have done exact same as u did:
    But i have this problem every time i follow it.
    form error: {'password_mismatch': 'The two password fields didn’t match.'}

  • @brightscience7581
    @brightscience7581 4 ปีที่แล้ว

    thanks man

  • @techyguys4394
    @techyguys4394 4 ปีที่แล้ว

    Well just cuious can it be done using one form

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

    Thank you man😁 finally I got it🙏

  • @jasminesharma5877
    @jasminesharma5877 4 ปีที่แล้ว

    What if we want multiple user to register...for example in a job portal webapp candidate and partner signup and login or in school management webapp student and teacher signup login
    how we do that.. could you please help..I am struggling in this for two week..😭😭😭 please Help

  • @jorjolandes
    @jorjolandes 4 ปีที่แล้ว

    Wow this is great, thanks! This is similar to signals right?

  • @pratikrane149
    @pratikrane149 4 ปีที่แล้ว

    In some articles, when extending user model using one-to-one field, I saw they also implemented post_save signals. You didn't seem to implement that. Is it really needed? what's the purpose of it, if may I know please?

    • @pratikrane149
      @pratikrane149 4 ปีที่แล้ว

      Hi Prit, because of certain requirement I had to extend the User model using AbstractBaseClass, and not using one-to-one field.
      Btw this video demonstrated on how to extend user model using one-to-one only with whole code too, didn't you find it helpful? No issues.
      There are mainly four ways to extend Django User model.
      1. Proxy Model
      2. One-to-One field
      3. AbstractUser
      4. AbstractBaseUser
      There is an old but amazing article which helped me a lot to grasp this overall topic, which I'm mentioning it below.
      simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html
      If you want a code then here I found a repo for you, which is just a basic django app demonstrating how to extend user model using one-to-one field.
      github.com/ankurbelbase/django-Extend-User-Model
      Hope it helps.

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

    I can't access the one to one relationship model using user.model_name.column name 😔😔

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

    Genius

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

    thank you so much for this video, is there source code of this video in your Github? because I couldn't find it, it would be really good if you share the source code in your git

  • @MHNahib
    @MHNahib 4 ปีที่แล้ว

    def __str__(self):
    return self.user.username
    is not working for me. What can I do?

  • @ZainAli-me4qk
    @ZainAli-me4qk 4 ปีที่แล้ว

    How to create serializers and views for this. Please tell me i need this

  • @zayed868
    @zayed868 4 ปีที่แล้ว

    what if I want first_name & last_name fields also in new model ?

  • @ameyneve
    @ameyneve 5 ปีที่แล้ว

    Hi Anthony, Could you please make video on 'Web Notifications' using Flask.? Thank you in advance

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      I'll see what I can do. Thanks for the idea.

  • @abdullahsarwar3139
    @abdullahsarwar3139 5 ปีที่แล้ว

    correct me if i'm wrong, isn't it discouraged to user "User". I think i read somewhere to use "settings.AUTH_USER_MODEL" instead?

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      Yes, if you plan on extending the model further or want more flexibility, you can use that instead, but the idea here will be similar.

  • @andreszu100
    @andreszu100 4 ปีที่แล้ว

    Excellent...Thanks

    • @prettyprinted
      @prettyprinted  4 ปีที่แล้ว

      You're welcome! Thanks for watching.

  • @TaslimaAkter-fk9in
    @TaslimaAkter-fk9in 4 ปีที่แล้ว

    How can I redirect to different pages based on user_type(customer,employee)?

    • @ZainAli-me4qk
      @ZainAli-me4qk 4 ปีที่แล้ว

      same question . did you found any solution?

  • @gauravshinde6559
    @gauravshinde6559 4 ปีที่แล้ว

    How to make it so the default user field is the currently authenticated user, Becoz my extended model will be lectures conducted by teacher.... Which he must add whenever he needs it

  • @etranger950
    @etranger950 5 ปีที่แล้ว

    hi thanks for your nice work .
    I have problem to save data on db, my register page is always refreshing when i click on submit button

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      Something should happen when the submit button is clicked. You may have to redirect to some other page after the registration is complete.

    • @etranger950
      @etranger950 5 ปีที่แล้ว

      @@prettyprinted Now it work, the problem was the name of the ''ExtendedUserCreationForm'' who i changed before

  • @victhour
    @victhour 4 ปีที่แล้ว

    nice tutorial. can you share the codes for this video?

  • @katerynaherashchenko1718
    @katerynaherashchenko1718 4 ปีที่แล้ว

    super! thanks!

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

    Thanks

  • @MrDayne-el3cc
    @MrDayne-el3cc 5 ปีที่แล้ว

    A have a question,so i made oneToOneField,with 3 new field(gold,ruby,power) how can i put default value without admin panel editing (i want get 3 defaul value for my 3 new field
    immediately after registration) (i speak london xD)

    • @MrDayne-el3cc
      @MrDayne-el3cc 5 ปีที่แล้ว

      btw, this is a game, i need set default stat to a new user, help please(

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

      When you create your class, you can add defaults. stackoverflow.com/questions/755857/default-value-for-field-in-django-model

  • @jibob5991
    @jibob5991 4 ปีที่แล้ว

    does anybody where is the video before this, like how he created username, email etc.

    • @pratikrane149
      @pratikrane149 4 ปีที่แล้ว

      There is no any video before this. This video itself demonstrates the complete tutorial.
      From 1:41 to 2:05. Here he extended the 'User' model which comes built-in with Django. This 'User' model comes with the necessary fields required for authentication like email, username, and password and also all the validation stuff is handled by django, which is the beauty of django!

  • @muhammadsaepudin6786
    @muhammadsaepudin6786 4 ปีที่แล้ว

    Thanksss!!!

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

    This is not worked for me!

  • @ZainAli-me4qk
    @ZainAli-me4qk 4 ปีที่แล้ว

    hi there i have question:
    i have three types of users with different form fields. should i need to create three profiles ? like i have sellers and clients and adminstrations . and how to verify user while login whether its seller or client?
    here is my user profiles.:
    class SellerProfile(models.Model):
    user = models.OneToOneField(
    User, blank=True, null=True, on_delete=models.SET_DEFAULT, default=None)
    # password = models.CharField(max_length=50, default=1)
    mobileNo = models.CharField(max_length=40, default=None)
    cnic = models.CharField(max_length=30, default=None)
    city = models.CharField(max_length=30, default=None)
    address = models.CharField(max_length=30, default=None)
    state = models.CharField(max_length=30, default=None)
    shop_name = models.CharField(max_length=30, default=None)
    def __str__(self):
    return self.user.username
    class ClientProfile(models.Model):
    user = models.OneToOneField(User, models.SET_DEFAULT, default=None)
    def __str__(self):
    return self.user.username
    here is my registration view:
    def registration(request):
    if request.method == 'POST':
    form = UserForm(request.POST)
    sellerFrom = SellerFrom(request.POST)
    if form.is_valid() and sellerFrom.is_valid():
    user = form.save()
    seller = sellerFrom.save()
    seller.user = user
    seller.save()
    username = form.cleaned_data.get('username')
    password = form.cleaned_data.get('password1')
    # user = authentication(username=username, password=password)
    # login(request, user)
    messages.success(request, 'account was created for' + username)
    form = UserForm()
    sellerFrom = SellerFrom()
    return redirect('login')
    else:
    form = UserForm()
    sellerFrom = SellerFrom()
    context = {'form': form, 'sellerForm': sellerFrom}
    return render(request, 'gui/signup.html', context)
    and here is my login view :
    def login(request):
    if request.method == 'POST':
    username = request.POST.get('username')
    password = request.POST.get('password')
    user = authenticate(request, username=username, password=password)
    if user is not None:
    auth_login(request, user)
    return redirect('home')
    else:
    messages.info(request, 'Username or password is incorrect')
    context = {}
    return render(request, 'gui/login.html', context)

  • @TheSheebapaul
    @TheSheebapaul 4 ปีที่แล้ว

    Hi, Thank you for this tutorial. When i submit the form i am getting this error. Could you please help. I have
    {% csrf_token %} in my page
    Forbidden (403)
    CSRF verification failed. Request aborted.
    Help
    Reason given for failure:
    CSRF token missing or incorrect.
    In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
    Your browser is accepting cookies.
    The view function passes a request to the template's render method.
    In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
    If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
    The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login.
    You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
    You can customize this page using the CSRF_FAILURE_VIEW setting.

  • @sikadijordan4061
    @sikadijordan4061 5 ปีที่แล้ว

    You are a héro

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

    Finally I have found the complete tutorial. Thanks man.