Answer the question
In order to leave comments, you need to log in
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")
...
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")
...
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"})
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question