Game Level data for each user in Database

Viewed 52

I'm creating a game in Django where there are like 500 levels. How do I store game level data for each user in database ( data can be stars for each level ). The game is more like the candy crush where each user earns stars for each level.

Is JSON suitable for this? Should I use a separate table for each level? Or should I use single table for all levels with level_id as Primary Key and user_id as Foreign Key?

1 Answers

My suggestion: If you just want to store the current level of each user - add a model that will save more details for each user and link it with OneToOneField.

you can see nice example here:

https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#onetoone

from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    level = models.IntegerField(default=0, blank=True)
Related