Django handling different databases per country - python

I have a question related to Django handling different databases. I have this doubt because, I need to create a database for different countries. And, my webapp has one dns, and only one. I want to be able to handle different countries with the same dns.
I have this questions in my mind but, I don't know which one is the best practice for this kind of situations...
1- In a Django Project create different apps, for each country and use a Database Router to differentiate. As:
if model._meta.app_label == 'customer_data':
return 'customer_db'
2 - Store a db name on session and when user logs in, sends every requests to the database related to the user. (don't have any clue of how to do this)
I don't know what else I can do, besides the ones I described above.
Any senior django developer, can help me with this question? and, if possible provide articles which can help me understand better... Thank you.

Related

Django architecture for different clients/technologies using same models

My team and I are having a hard time defining the architecture for my backend environment using Django.
To contextualize a little, we built an application similar to an App Store, for all our developments to be displayed, with the difference that it also launches the apps.
To maintain all the information about this apps, we built this backend in Django that is basically a database accessed through an API from the App Store, and a Django Admin website to register the informations to be delivered in the API (the one Django automatically generates).
The thing is, today, we have only one technology involved and therefore only one database and one Django Admin. But, other areas in our company want to use this architecture we built, with other apps and therefore other informations, not related to our environment.
The question is, should we deploy several Djangos in different ports of our server? Or should we try to create a dependency in our models to a "Technology" related to it? (For this option it is important to note that our architecture is complex and highly nested). Or maybe should we use the multiple databases provided from Django?
Has anyone had a situation similar to this one?
Thank you in advance!
The problem for me with creating a dependency in our models to a "Technology" is has to do with what OP means with other informations which are not related to OP's environment. Is OP able to create a database model that works, whose system is not overly complex and that can accommodate for all of the cases OP wants? If that's the case, I don't see why not.
There's an interesting Q&A talking about having multiple instances of Django in the same server. Personally I have already deployed in virtual shared hosts whose logic was essentially that, with the limitation that OP is sharing with other people too. OP can see in that thread that there are various cases where it was a viable option. Personally I find such shared hosts only viable in initial stages as they weren't as performant... but a great way to save resources if one is working with tight budgets. IMO this can be one of the best options for OP's case... which is considered single-tenant.
In regards to multiple databases, this automatically leads me to an approach to multi-tenant applications (within the multi-tenant applications there are approaches where one only needs one database as well). OP wants to go for this case if OP's application can serve all of the users from the same host, which doesn't seem the case for OP, unless OP is able to create a dependency in OP's models to a "Technology" (as addressed in the first paragraph).

Implement Django oauth to 2 Different User Models

Is there any way to have Django oauth for 2 Different types of User Models?
I am working on a site which has Customer and Business, and both will have their seperate logins. There is no such information that they will have in common and really can't use common db table for both.
We will be having different database tables for customers and business, thus need different oauth tables. I couldn't find any way to accomplish it.
try django-role-permission
https://django-role-permissions.readthedocs.io/en/stable/
This has role based permission. If you go through the example, probably you will find exactly you are looking for..good luck

Change Database used in Django session based on User logged on

I want to give access to the Django Admin to some people, but only have them be able to read (no update/delete/create powers). Unfortunately this seems largely unsupported in Django.
The solution my team came up with was to use Multiple Databases, and have one of the configurations be a read only one. However, is there a way to say somewhere in Django "Get the database that X user is assigned, and read from that" ?
(Also if someone has a non painful suggestion on how to do the read-only admin, I would appreciate it. The use case is just that there is a lot of data that needs to be searched through and looked at by people, and the admin tool already has this installed).

How to use same django project multiple times - each with diffrent database?

I wrote a django project that is some kind of a CMS.
Now, I want to be able to create several accounts that use that CMS, Each with a different database.
For example, user can create himself an account in my service - and he will get a site based of that CMS.
How can I get started doing this?
Look at the django docs https://docs.djangoproject.com/en/dev/topics/db/multi-db/ There are is useful example. Another good article https://thenewcircle.com/s/post/1242/django_multiple_database_support
Unfortunately Django is not suited to dynamically switch databases at runtime. You either have to implement really hackish solutions (like mentioned in this question Django multiple and dynamic databases ) or to go with several independent Django instances which you will have to start up on your server dynamically.
A much simpler solution would be to stick to one database and distinguish different users' content by some other means, like Django Sites framework. The only problem with this approach in my opinion is that you will have to carefully set up your admin site configuration, so that users don't see each other's objects (in case you planned to use built-in Django admin).

Storing/Retrieving/Editing project specific data with Django

I'm new to Django and I'm working on the public website for a small company.
I'm facing an issue that I guess has already been encountered by lots a django noobs,
but I can't manage to find a good solution.
My problem is that there some informations (contact address, office phone number, company description...) that I use in nearly all of my views and are by nature unique (undertand: a database table with only 1 row). I currently store these informations has a model in my databse, but I find it a bit weird issue an additional database request each time (each view)
I need to access them. However, I need my client to be able to edit these informations (by the admin interface).
So, please, is there a django idiom to handle such an use case ?
Thx in advance.
If you look into caching solutions, they will probably do what you need.
The general queryset caching solution I use in johnny-cache, but for what you need, you can probably just load it up from the db and store it in the cache.
What you want to do is use select_related('contact_profile','office_data') etc when you query the items in your view, and in the admin, instead of registering all the data separately just use the InlineAdmin class for the Admin site and you will be able to edit all the information as if it was a single entity.
Check out the django docs for more information.

Categories