Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make separate database for every user in Django?

So I am new in programming in general and I am currently learning Django. I know it may be a stupid question, but I don't seem to find the answer anywhere and I really want to know it.

I am making a basketball stats tracker app. How this works is you enter how many times you scored out of how many attempts and it adds it to your current accuracy percentage. Let's say last time you were 5/10 and now you are 35/90. Then the percentage goes from 50% to 40%. It works for me but I want to make it so people can sign up. I know how to do that but how do I make the stats personalized for every user?

like image 678
Georgi Dimitrov Avatar asked Oct 17 '19 20:10

Georgi Dimitrov


1 Answers

Creating a new database for every user is not really how databases work. You should do this using tables.

Every model in Django by default gets a field called 'id', which is what the databases uses to tell different entries apart. Every user you create gets a new id. So the first user gets id=1, second user gets id=2 and so forth. These are all stored in the database in the users table. So each user that wants to track their score will get their own record in the users table. A basic example of what such a table would look like:

----------------------
| id | name    | age |
----------------------
| 1  | John    | 23  |
| 2  | Mitch   | 15  |
| 3  | Joe     | 33  |
...

In reality, Django uses a special user model for this. By default Django has a specific model it uses. More on that here. Given that you've already implemented authentication, you should be well on your way with this. Now, to track the scores of users you could create another model in your app called something like 'Score' that may look like this:

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

class Score(models.Model):
    user = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
        related_name='scores',
    )
    tries = models.PositiveSmallIntegerField()
    successful_tries = models.PositiveSmallIntegerField()

Something along those lines. At this point, pay no mind to the 'get_user_model()' method call. This is a new model that tracks scores of users. You can create one by doing Score.objects.create(user=request.user, tries=10, successful_tries=5). Every training session you have you can create a new 'Score' object that tracks how many tries you had, and how many were successful tries. You can access all of them using request.user.scores.all() (that's what related_name does). This way, one user can have 0 to infinite 'Score' objects they are related to. Each user residing in the users table of your database, and each score residing in the scores table of the database.

If this is a little confusing, check out this blogpost on the topic. Database design, and setting up your domain in general, are important concepts to learn if you're starting out.

like image 113
timmysmalls Avatar answered Oct 20 '22 00:10

timmysmalls