Django Tutorial #27 - Foreign Keys
ฝัง
- เผยแพร่เมื่อ 24 ส.ค. 2024
- Hey gang, in this Django tutorial I'll explain the concept of foreign keys, which allow us to associate one model with another. In our example, we'll associate an article with a user.
DONATE :) - www.paypal.me/...
----- COURSE LINKS:
+ Python tutorials - goo.gl/xD2AvX
+ Course files - github.com/iam...
+ Django docs - docs.djangopro...
+ Atom editor - atom.io/a
+ CMDER - cmder.net/
======== Other Tutorials =========
----- NODE.JS TUTORIALS
• Node JS Tutorial for B...
----- MONGODB TUTORIALS
• MongoDB Tutorial for B...
----- SUBSCRIBE TO CHANNEL - / @netninja
======== Social Links ==========
Twitter - @TheNetNinja - / thenetninjauk
Patreon - / thenetninja
Truly thank you, 2024 and this is still the most useful Django video series I've seen
Thanks a lot ..another great video .....just a quick hint for others...in django2 this will give an error(Foreignkey requires 2 arguments not 1 as django 1.9 used to) . just edit the author field line to: author = models.ForeignKey(User,on_delete=models.CASCADE,default=None) or . author = models.ForeignKey(User,on_delete=models.PROTECT,default=None)
thanks for hint but what is actualy on_delete=models.CASCADE
CASCADE means when the referenced object is deleted(User in this case), also delete the objects that have references to it (Articles in this case). because each user has articles that he/she created, so if this user doesn't exist anymore then for sure there should not be articles that belong to this user .
remember this is a Foreign key so it is related to a primary key in another table (this is something more related to Database). I hope this will help..thanks
I found this helpful -
stackoverflow.com/questions/38388423/what-does-on-delete-do-on-django-models
Thanks
Thanks, dude
When I makemigrations, I get this error in cmd - TypeError: __init__() missing 1 required positional argument: 'on_delete'. This is an issue if you have django 2.0 installed.
Resolution: pass the following perameters for author as on_delete is a required perameter in 2.0 ---- author = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
thanks, I had the same error
thanks it was helpful too
One year but no solution to this problem??
@@Commonaadmi768 He says how to resolve it.... " Resolution: pass the following perameters for author as on_delete is a required perameter in 2.0 ---- author = models.ForeignKey(User, on_delete=models.CASCADE, default=None)" ..... But to figure out a problem we need ALOT more information. Realize how many connections there are just to make a view. right now my form isn't posting to my page for some reason and research is failing me so I have to read the docs and attempt to teach myself exactly how everything works to find the error.
thanks a lot
For django 3.0, use this:
author = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
Thanks very much. It was really helpful
Thanks so much for this series. I have learned a ton, and it has been really interesting. Plus, your explanations are really clear, and so it is easy to follow. I will recommend it to friends and colleagues. I look forward to the next one.
I love this tutorial. It has helped me a lot in 2022.....
Thank you! Spent hours trying to figure out how to pass an instance of the user into the foreign key field.
Hey guys if this still doesn't save your blog add two more parameters to the author field like author=models.Foreignkey(User,default=None,blank=True,null=True,on_delete=models.CASCADE)
this should probably work!
Thanks :)
what these blank=True,null=True used for? What means?
Nice ! ...
tips
-Add on_delete thing to foreign key
-Careful with usage of User and user
-there is nothing to check if an article with same name/slug exists .. So if you do .. You'll get the GET error saying it got 2 things
sir,on_delete =''', what value we should give on this field
One heads up guys , if you are working in pycharm it automatically saves all the changes in code and you won't be able to migrate before you delete all your articles. so if this shows up comment out the code in models and delete all the articles.
you just saved me hours of work, thank you very much mate
Thank you very much! Going to reach 300K subscribers soon! I am sooo glad! CONGRATULATIONS!
Thank you :D
You Are AWESOME thank you
Exactly what I was searching for
Omg big help on solving my error. Thank you
Awesome, glad I could help! :)
Thanks for a great video! I've been trying to figure this out and finally I saw your video after searching and searching. Works like a charm! Thank You!
thanks nicely explained
you don't have to delete your old articles when you change the model. You can just set null=true for the new field.
Oh, man thanks a ton🙏🙏🙏.
This was Much needed for me tbh.
Thanks It helped me a lot😀
Helped a lot! Thanks
if anyone getting " TypeError: Field 'id' expected a number but got None " error then try Delete all files except 'init' in migrations folder and do makemigrations, migrate again... that worked for me
thanks a lot for your videos! very useful.)
I like this way of carrying out the transaction and passing to the user, but is it the best practice? or is there a better one
There is an error when I make the migration: ForeignKey.__init__() missing 1 required positional argument: 'on_delete', perhaps it caused due to the version and to fix this just add another parameter on_delete like this:
author = models.ForeignKey(User,on_delete=models.CASCADE)
Hope this can be helpful.
In the admin page, in "articles", in the drop-down menu for "author", how does Django know what field of the author to show (id, first_name, last_name etc.)?
In my case migrations always failed ...with error no migrations need to apply
After running server I am getting no such column
PLEASE PLEASE HELP
1:35 tao foreign key trong model (articles) de luu article moi vao trong user
4:58 them code vao ham de luu form do client tao
Thanks a lot, great explanation. Searched hours for this topic.
does it worked?
As Simplified As Butter
Awesome. Thank you
great content!
Thanks so much!!!!
is there any way to get the foreignkey In ascending order of table instead of default=none ?
thanks a lot
I have a question, where do 'instance' know that it is related to the model Article?
delete all articles and
add this line to models.py
author = models.ForeignKey(User, on_delete=models.CASCADE,default=None)
Its not working for me. I am getting an error 'django.db.utils.OperationalError: no such column: sign_up_post.author_id'
sign_up = my moduke name
post = class name in models.py
author = field name
TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime',im getting such type error ,can somebody fix this problems please
are you from Howgwaarts?
Thank you very much your greate
what if I cannot use the superuser I created before? I am not sure why, but just say it not authenticated to admin page, how can I log in the admin page again?
create another superuser
user can save multiple artical or just one?
Can someone give me the code please ..love you courses
But what if I don't have any form in views.py?
How do we implement search functionality on this app?
What if I wanted to show the id of a user instead of the username, inside the select box of the backend? So instead of having mario etc as options, it would show the user id and use that as the foreign key, is it possible?
thx
its showing return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: new__articles_article.author_id
what should i do?
I don't think you still have a problem. but if have the go-to migrations folder and delete the last migrations with name 000_autho_blah manually and the go-to the admin site and delete all the articles i had the problem cause i thought i am genius and not deleted all the articles from database get more help here:
stackoverflow.com/questions/42733221/django-db-utils-integrityerror-not-null-constraint-failed-products-product-ima
awesome
Having such a difficult time with getting the damn thing to post to the page. Did you remove Django's built in Permission system for your users? I'm wondering if that is why I can't get the users stuff to post. I have an admin_posts app and it can only be posted to from the admin site BUT the posts actually show up as expected. So I've narrowed it down to something not getting sent (via the view) properly. Earlier I got the publish date to show up but no text. All the while the posts show up in the admin page (when you go to your page posts) Text and all. I cannot find a single thing on this everyone.....I'm not even joking when I say I've been reading docs and researching this for 18 hours yesterday (a good portion was spent on making sure users can register and such) and 10 the day before. This is going to be a large project and I can tell I'm gonnna have to redo it so many times. Thank god for backups right? (I even have backups hidden on my hard drive in case I accidentally modify a back up or get drunk and try to save working but fucked up code over top of the backups.......I love programming and this is one reason, this is serious fucking work if you actually want to do this. It's not actually easy because, well look at everything you have to learn. almost a month in and though I understand SO MUCH about how django works and understand waaay better on how python works [I'm using python 3] that I actually think I might be able to get a job in this one day, hopefully soon.)
Good luck everyone, keep your heads about you when an error comes up and you can't figure it out. It will come eventually and don't let yourself get burnt out, if you start dreading working on your project, stop for a day or two. DO NOT LET YOURSELF GO BACK TO IT until you actually want to and the thought of attempting to fix the errors doesn't piss you off or upset you (you can be upset because Errors fucking suck but try not to actually get mad)... WELL THAT WAS A NICE BREAK FROM FUCK UP CITY lmao Wish me luck y'all
print("Good luck and happy coding!
")
print("P.S PYTHON IS GREAT")
Wizardry!
6:03
From where is the user coming to the request?
2:29
Can you do this in Class Based View?
In CreateView?
I have been trying to figure out but I can't. If you don't mind, please give me the way I can work this out.
I'm stuck there as well. Let me know if you have found any solutions, I hope you did.
Hi Shaun, please complete this series fast, it's almost so late for django, looking for next series egarly
Can you please do a vid on jsonwebtoken by making it require a token to post and also being able to reset the token
how to save the user entered data in database... u have not written code for saving..
That sounds like British.............