Django Tutorial #27 - Foreign Keys

preview_player
Показать описание
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.

----- COURSE LINKS:

======== Other Tutorials =========

----- NODE.JS TUTORIALS

----- MONGODB TUTORIALS

======== Social Links ==========

Рекомендации по теме
Комментарии
Автор

Thanks a lot ..another great video 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)

wisamkhalid
Автор

Truly thank you, 2024 and this is still the most useful Django video series I've seen

toctoc
Автор

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)

jimmyjimmy
Автор

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.

jdevcast
Автор

For django 3.0, use this:
author = models.ForeignKey(User, on_delete=models.CASCADE, default=None)

tasnimzia
Автор

I love this tutorial. It has helped me a lot in

givansot
Автор

Thank you! Spent hours trying to figure out how to pass an instance of the user into the foreign key field.

dlshapiro
Автор

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

Vengen
Автор

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!

amongdoomers
Автор

<3 bro your voice is so nice, i watch another tutorials and i get headache

altair
Автор

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!

nosnibornek
Автор

Thank you very much! Going to reach 300K subscribers soon! I am sooo glad! CONGRATULATIONS!

sparklingtwilight
Автор

you just saved me hours of work, thank you very much mate

emiktra
Автор

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.

almuntasirabir
Автор

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.

magickaito
Автор

Omg big help on solving my error. Thank you

likjou
Автор

you don't have to delete your old articles when you change the model. You can just set null=true for the new field.

chimerablack
Автор

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

dhrutijoshi
Автор

Oh, man thanks a ton🙏🙏🙏.
This was Much needed for me tbh.

bsmaheshkumar
Автор

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.)?

GrumpyOldMan