Dynamic database selection based on URL in Django - python

Let's say the first page of the app has two links. Is it possible to pick the database depending on which link is clicked? The databases both have the same models, but different data. For example, let's say the application contains students for different colleges A and B. If link for A is clicked, then database for A is used which contains the students for college A. The entire application after this point should use database for college A.
I understand there are ways to work around this problem by just designing the databases differently, i.e. having a college field, and just filtering out students with the particular college affiliation. But I am hoping to find a solution using Django to just use two different databases.

So you need to store the chosen database in session or smth and you can easily pick the database. From the docs
>>> # This will run on the 'default' database.
>>> Author.objects.all()
>>> # So will this.
>>> Author.objects.using('default').all()
>>> # This will run on the 'other' database.
>>> Author.objects.using('other').all()

You could mix together Database Routers with this GlobalRequestMiddleware solution to create a database router which inspects the request to figure out which database to use.
class RequestDatabaseRouter(object):
def db_for_read(self, model, **hints):
request = GlobalRequestMiddleware.get_current_request()
key = self.get_database_key(request) # Implement get_database_key
return key
There may be some way to inject the request into **hints, which I think would be preferable over the Middleware solution, however, I'm unaware how to at the moment. You may need to make sure the GlobalRequestMiddleware is called after the AuthenticationMiddleware otherwise you may not have the user on the request to inspect.

Related

How does a django project use multiple databases with the same structure to read and write, distinguished by url

I have a django project and want to divide it into multiple databases with the same structure.
Use url to distinguish different databases. When the admin management page logs in, log in to different databases according to different urls.
For example: 127.0.0.1/admin uses the admin database, 127.0.0.1/admin2 uses the admin2 database.
Does django implement this function? What do I need to do, Can you give me some suggestions or ideas? thank you very much
TL;DR
As far as a single django project is considered, there is no default way to achieve multiple database.
Scenario 1
From your very limited explaination I will assume that you want to seperate data of one admin dashboard from the data of second admin dashboard, to achieve data isolation with respect to permissions & other models, this is called multitenancy.
Very briefly: In a Multitenant architecture you can have multiple tenants whose structure is defined by your models.py and you can control all this tenant via a main superadmin, these tenants can have their own admin dashboard where the data stored in them are only specific to their tenant users. In more simpler terms you can have a SaaS app with this method, where you can have multiple organizations and these organizations have their own users with their specific permissions/groups.
Multitenancy can be achieved in django via a Schema seperated database using POSTGRESql and this awesome package that has already done most of the heavy lifting for you. You can achieve seperate logins via url or subdomain. If your tenants have users who part of more than one organisation and you want a single login for all of them then you can use this package that goes along with django-tenants. It provides a public user table with permission modules separate for each tenant.
Scenario 2
From your very limited explaination I will assume that you still want seperate databases for your app, in such case you need to rethink your approach to the problem because it is not something you will fancy after deployment as there is not direct way provided by django. Instead you should look into micro-service architecture.

How to keep your code clean with denormalized data?

I'm going to ask this question in two parts: first the general question, and than the question for my specific use case.
The general question:
I'm building a podcast app, where, hopefully, we'll have users. Users have subscripitons, settings, ... , which I'd like to store on the User object, but subscriptions and settings don't belong in the same module in my code.
How do you structure your code so that all the relevant data about a user is stored together, but the code that defines and deals with specific properties can be separated?
My specific use case
I'm building the back end on Google App Engine. My user class looks something like this:
class User(ndb.Model):
username = ndb.StringProperty(required=True)
email = ndb.StringProperty(required=True)
...
Now I could just add another property for subscriptions, settings etc, but these definitions don't really belong in the users module. I've tried defining a SubscriptionsHolder and SettingsHolder class using ndb.PolyModel, but with multiple inheritance, only queries on the last superclass in the User definition supports querying.
I could just make the settings and other module query the User model directly, but this results in a circular dependency, where the User the users module depends on settings for subclassing, and settings depends on users for querying. I know I can resolve the circular dependency by moving the import statement around, but that just seems like a hack to me.
My approach was to treat User and Settings data as separate but related collections. Instead of subclassing or using PolyModel I simply introduced a way to imply a 1:1 relation between those data sets.
One way is to add a KeyProperty to Settings that links back to User. Another way is to create each Settings entity with the same id/name that is used by the related User entity. This second way allows a direct Settings.get_by_id() call once you have the User key.

Django Subdomain

I'm trying to make a basic store app. I've set up a database so that every product is tied to a particular store: let's call the stores Shoes, Toys, and Books.. I need to set up subdomains for the app (it's in the assignment specs, no choice there) so that I can map to shoes.myapp.com, toys.myapp.com and books.myapp.com. What I think I need to do is somehow set up the subdomain (which I've googled but am confused about: is this the way to go?) and then, I guess, filter my databases from the info in the subdomain so that only products that have the store name "Shoes" for example appear on the page. Am I anywhere approaching the right track or is there a much better way to structure this?
I suggest you to use this application: django-subdomains. http://django-subdomains.readthedocs.org/en/latest/index.html
And then, in your settings.py, you should use:
SUBDOMAIN_URLCONF = {
'toys': 'yourproject.urls.toys',
'shoes': 'yourproject.urls.shoes'
(...)
}
If you need to use the name of the subdomain in a view, it will be attached to the request object:
def your_view(request):
subdomain = request.subdomain
products = Products.objects.filter(store=subdomain) #an example how to use it to specif database queries. I dont know how your models are

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.

Django Admin relations between tables: save database updates in several tables

I am using Django admin for managing my data.
I have a Users, Groups and Domains tables.
Users table has many to many relationship with Groups and Domains tables.
Domains table has one to many relationship with Groups table.
and when I save the User data through admin I also need some addtional database updates in the users_group and the users_domains table.
How do I do this? Where do I put the code?
I think you are looking for InlineModels. They allow you to edit related models in the same page as the parent model. If you are looking for greater control than this, you can override the ModelAdmin save methods.
Also, always check out the Manual when you need something. It really is quite good.
The best way to update other database tables is to perform the necessary get and save operations. However, if you have a many-to-many relationship, by default, both sides of the relationship are accessible from a <lower_case_model_name>_set parameter. That is, user.group_set.all() will give you all Group objects associated with a user, while group.user_set.all() will give you all User objects associated with a group. So if you override the save method (or register a signal listener--whichever option sounds stylistically more pleasing), try:
for group in user.group_set.all():
#play with group object
....
group.save()

Categories