Set Up the Foreign Key
Learn and practiceforeign key setup to create a relationship on a model.
We'll cover the following...
The listings on the website are not tied to any users yet. Users don’t want to see other peoples’ listings, so we’ll use a foreign key to keep track of which users created which listings.
A foreign key is a way to create a relationship in a model. In our case, the relationship is between the listings and the users. Each listing will belong to a specific user who created the listing.
Listings linked to a user
Update the model
The updated model (listings/models.py) is shown in the following widget:
""" ASGI config for example project. It exposes the ASGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ """ import os from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings') application = get_asgi_application()
Setting up the foreign key
In line 23, the Listings
model is linked to the User
model by means of a foreign ...