Django multi-user saas

倾然丶 夕夏残阳落幕 提交于 2019-12-09 21:50:07

问题


I'm creating a small saas for learning purposes... I know that the best way to go is to have a separate schema for each user... But my saas is a small app and I really don't need separate schemas plus it would complicate stuff (later alterations to the schema)... So I'm going to use the same schema for all users and just append each user's ID to the entry and then display only the user's entries upon login.

Is there a way I can "prepend" user's ID upon login through middleware? Or do I need to adjust all the queries (filter by user's ID)? I already store the user's ID to his entries... so I need this only for select's.

Thank you!


回答1:


I don't know of any way to automatically filter querysets based on request.user. This is because the ORM is not coupled with django.contrib.auth.

I tried two SaaS designs:

  1. 1 database for all sites, and manual filtering on all querysets. Of course this made django.contrib.admin not possible out of the box. It might be possible to enable django.contrib.admin with a really tight setup of django-authority or maybe even other django per-object permission packages (google cached version because djangopackages.com is down ATM).

  2. 1 database per site, that's just easier, keeps the code cleaner, and enables django.contrib.admin out of the box, which can be really cool if you install a django admin theme. Also I enabled django.contrib.admindoc as well as django-dbtemplates.

(Apparently in your case, 1 site = 1 user, but the concept is the same)

I prefer the second implementation because it's obviously a lot less development work, which is compensated by a little system administration.



来源:https://stackoverflow.com/questions/8740311/django-multi-user-saas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!