Adding security-related data to the models
We will associate a game with a creator or owner. Only the authenticated users will be able to create new games. Only the creator of a game will be able to update it or delete it. All the requests that aren't authenticated will only have read-only access to games.
Open the models.py
file in the games_service/games
folder. Replace the code that declares the Game
class with the following code. The new and edited lines are highlighted in the code listing. The code file for the sample is included in the restful_python_2_07_04
folder, in the Django01/games-service/games/models.py
file:
class Game(models.Model): created = models.DateTimeField(auto_now_add=True) name = models.CharField(max_length=200, unique=True) esrb_rating = models.ForeignKey( EsrbRating, related_name='games', on_delete=models.CASCADE) release_date = models.DateTimeField() played_once = models.BooleanField(default=False) played_times...