F
F
FeNUMe2014-03-11 20:40:02
Django
FeNUMe, 2014-03-11 20:40:02

What is the optimal model for the described task (Django)?

Good afternoon. I am currently developing a small service that will provide access to certain data, the data is divided by country, and I need to give different users access only to certain countries for a certain period. Checking whether there are rights to access the country database occurs every time the user requests data. At the moment, I see several options for solving this problem:
1a. Own user model with fields for each country, which will record the date before when access is active.
+ Simple implementation
+ Only 1 query to the database (which in any case is performed to obtain other data on the user)
+ Simple migration when adding additional countries
- A bunch of extra fields in the user model

class MyUser(AbstractUser):
    ru = models.DateTimeField(default="2000-01-01 00:00:00.00+00")
    uk = models.DateTimeField(default="2000-01-01 00:00:00.00+00")
    ...

1b. The essence is the same, only all countries are placed in a separate table associated with the user
+ The user model is not littered
+ In sections where verification of rights is not needed, we do not receive unnecessary data.
- Extra join in every data request
class Permissions(models.Model):
    user = models.ForeignKey(User)
    ru = models.DateTimeField(default="2000-01-01 00:00:00.00+00")
    us = models.DateTimeField(default="2000-01-01 00:00:00.00+00")
    ...

2. We add only 1 field to the user model, in which we store the "country - date" dictionary.
+ The user model is not littered
+ For 1 request we get all the necessary rights
- More complex logic in the views and templates for checking rights
- Adding / deleting countries becomes much more complicated - you will have to update the field for all users (the main problem)
class MyUser(AbstractUser):
    perms = JSONField(default=lambda:{"us":"2000-01-01 00:00:00.00+00",
                                      "ru":"2000-01-01 00:00:00.00+00"})

Maybe there are better ways that I'm missing? And if not, which of the proposed methods do you think is better or how can they be optimized?
PS At the moment there are 25 countries, I expect no more than 5-10k users, the requests for data from the country database are quite heavy.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yeletsky, 2014-03-11
@Tiendil

Of the options presented, I would choose 2. There are actually no problems with adding new countries - it is enough to assume that if the country is not in the dictionary. then it corresponds to some default date. I don't see any complication of the check logic either.
Why is there no option for a simple model like [user_id, country, date]? It is convenient to make checks and selections.
> users I expect no more than 5-10k
For what period of time? We need to look at peak loads.
If this is a total number, then most likely you don’t need to worry about the load at all, but do it as convenient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question