Difference between revisions of "Relating models in Django, ie Primary key to Foreign Key"
(Created page with "To complete this tutorial, you must have created a Product model in the previous tutorials. =models.py= Within your app folder, find the 'models.py' file and edit it, It shou...") |
(→models.py) |
||
Line 21: | Line 21: | ||
return self.author.username + ' - ' + self.product.name + ' - ' + str(self.rating) + ' out of 10' | return self.author.username + ' - ' + self.product.name + ' - ' + str(self.rating) + ' out of 10' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | =Register the Model= | ||
+ | Now in the same folder as the 'models.py' find the 'admin.py'. It should currently have this code: | ||
+ | |||
+ | <syntaxhighlight lang=python> | ||
+ | from django.contrib import admin | ||
+ | from .models import Product | ||
+ | |||
+ | admin.site.register(Product) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | We now need to register the 'Review' model, so alter the code to this: | ||
+ | |||
+ | <syntaxhighlight lang=python> | ||
+ | from django.contrib import admin | ||
+ | from .models import Product, Review | ||
+ | |||
+ | admin.site.register(Product) | ||
+ | admin.site.register(Review) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Now sung the admin program, you will need to run the manage, and then migrate. You will then need to do the same and this time run makemigrations. | ||
+ | |||
+ | =Checking your model= | ||
+ | |||
+ | run the server and visit http:\\127.0.0.1:8000 and you should see your web app. In the address bar add '/admin' and login. You should see you model listed on the dashboard. You should be able to create new entries from this admin dashboard. |
Revision as of 08:57, 18 September 2019
To complete this tutorial, you must have created a Product model in the previous tutorials.
models.py
Within your app folder, find the 'models.py' file and edit it, It should already contain a model for 'Product'. Firstly, we are going to have 'User' as one of the foreign keys so we must import the built in 'User' model:
from django.contrib.auth.models import User
Now for the model itself, enter the following:
class Review(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
rating = models.IntegerField(validators = [MinValueValidator(0), MaxValueValidator(10)])
text = models.TextField()
date = models.DateTimeField( default=timezone.now )
def __str__(self):
return self.author.username + ' - ' + self.product.name + ' - ' + str(self.rating) + ' out of 10'
Register the Model
Now in the same folder as the 'models.py' find the 'admin.py'. It should currently have this code:
from django.contrib import admin
from .models import Product
admin.site.register(Product)
We now need to register the 'Review' model, so alter the code to this:
from django.contrib import admin
from .models import Product, Review
admin.site.register(Product)
admin.site.register(Review)
Now sung the admin program, you will need to run the manage, and then migrate. You will then need to do the same and this time run makemigrations.
Checking your model
run the server and visit http:\\127.0.0.1:8000 and you should see your web app. In the address bar add '/admin' and login. You should see you model listed on the dashboard. You should be able to create new entries from this admin dashboard.