Django multiple Project using same database tables - django

I've been reading through related articles regarding using same database for multiple django projects however, I have not been able to come up with a fix yet.
I've tried relative and absolute pathing when importing but it gives "attempted relative import beyond top-level package" error when I try to access parent directory.
Project 1 gets the users to write to database by filling in a form and Project 2 retrieves data written in by users from database.
I'm using Postgresql for database. I've tried writing exactly same models.py for both projects but it seems like in database they appear as separate relations/tables. E.g. for table named school in both models.py, it would look like project1_school and project2_school in Postgres database.
Is there a way to write to and read from same tables of same database?
Thank you so much in advance.

I think that you might be confuse with the difference between Projects and Applications.
Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.
Writing your first Django app, part 1
So in you particular case, I would say that your actual projects, both of them, could be applications of one project. The main reason, why I think this is a better approach, is that both are gonna use the same data, one application writes while the other retrieve it. One could even argue that they actually could be the same application. But this may depend on many factors of your business.
BTW, is really hard for me to imagine a situation where it would be a good idea to have two projects using the same database. Even if both projects need to share data, I would not think in using on database. I would try to solve it at an application level. But I you need for some reason to share information at database level, there are tools to connect both databases.

Related

Django, common model used in several applications

In my project, there are applications named "developer" and "startup"
In developer and startup application, i need a table named "languages", which implies that the language developer use and startup use.
So i made model named "languages", But I'm thinking about whether to make this in developer application or startup application, or make a new application and make it in it.
When I made it in developer application, i could use it by importing startup application models.py. But actually, I don't think that the table "languages" is more relevant to application "developler" than "startup".
How should I do?
I referenced link https://stackoverflow.com/questions/4137287/sharing-models-between-django-apps
As far I can see from your context: the language model (If even needed) should go to the developer because the developer is the one who closely relates to language, and will mostly consume it in views, and serializer, etc.
In general, we follow these three basic rules
Keep apps small Each app
Does one thing well
Keep apps separate so they can be exported and used elsewhere
Just keep these simple basic rules in front of yourself whenever defining a new model in a Django app.

Django two projects, same database

I try to set up two projects,
The first is a management project - it involves auth_user, sessions, writing to db.
The second project is a "serve content" project, it only read from the db, uses no sessions, users..
It rely on db data created in the first project, but does not change it.
The second project urls will be accessed very frequently. That's why I want to seperate the projects, with different subdomaons, and two apache instances, giving the second one more "power" (processes in wsgi deamon conf).
So what should I share between the projects? Same settings? SECRET_KEY? Models?
I assume I should also remove session app.
Can I set django db to be in read only mode?

Deploying Django as standalone internal app?

I'm developing an tool using Django for internal use at my organization. It's used to search and tag documents (using Haystack and Solr), and will be employed on different projects. My team currently has a working prototype and we want to deploy it 'in the wild.'
Our security environment is strict. Project documents are located on subfolders on a network drive, and access to these folders is restricted based on users' Windows credentials (we also have an MS SQL server that uses the same credentials). A user can only access the projects they are involved in. Since we're an exclusively Microsoft shop, if we want to deploy our app on the company intranet, we'll need to use an IIS server to deal with these permissions. No one on the team has the requisite knowledge to work with IIS, Active Directory, and our IT department is already over-extended. In short, we're not web developers and we don't have immediate access to anybody experienced.
My hacky solution is to forgo IIS entirely and have each end user run a lightweight server locally (namely, CherryPy) while each retaining access to a common project-specific database (e.g. a SQLite DB living on the network drive or a DB on the MS SQL server). In order to use the tool, they would just launch an all-in-one batch script and point their browser to 127.0.0.1:8000. I recognize how ugly this is, but I feel like it leverages the security measures already in place (note that never expect more than 10 simultaneous users on a given project). Is this a terrible idea, and if so, what's a better solution?
I've dealt with a similar situation (primary development was geared toward a normal deployment situation, but some users have a requirement to use the application on a standalone workstation). Rather than deploy web and db servers on a standalone workstation, I just run the app with the Django internal development server and a SQLite DB. I didn't use CherryPy, but hopefully this is somewhat useful to you.
My current solution makes a nice executable for users not familiar with the command line (who also have trouble remembering the URL to put in their browser) but is also relatively easy development:
Use PyInstaller to package up the Django app into single executable. Once you figure this out, don't continue to do it by hand, add it to your continuous integration system (or at least write a script).
Modify the manage.py to:
Detect if the app is frozen by PyInstaller and there are no arguments (i.e.: user executed it by double clicking it) and if so, then run execute_from_command_line(..) with arguments to start the Django development server.
Right before running the execute_from_command_line(..), pop off a thread that does a time.sleep(2) (to let the development server come up fully) and then webbrowser.open_new("http://127.0.0.1:8000").
Modify the app's settings.py to detect if frozen and change things around such as the path to the DB server, enabling the development server, etc.
A couple additional notes.
If you go with SQLite, Windows file locking on network shares may not be adequate if you have concurrent writing to the DB; concurrent readers should be fine. Additionally, since you'll have different DB files for different projects you'll have to figure out a way for the user to indicate which file to use. Maybe prompt in app, or build the same app multiple times with different settings.py files. Variety of a ways to hit this nail...
If you go with MSSQL (or any client/server DB), the app will have to know the DB credentials (which means they could be extracted by a knowledgable user). This presents a security risk that may not be acceptable. Basically, don't try to have the only layer of security within the app that the user is executing. The DB credentials used by the app that a user is executing should only have the access that the user is allowed.

Two Django projects with common models and business logic

I have two Django Projects that have different use cases. There are reached using different domains. They are hosted in two different servers. Also each Django project has it's own database.
Now, both the projects have some models and some business logic common between them. I don't want to duplicate the code and data which shall be chaotic going forward. Also, I want the models and code (business logic) to be in sync (when models/code is altered).
Can anyone guide me towards a pattern that can help me attain the required architecture: 2 separate projects with common models and business logic.
Thanks in advance.
I've done this before. You will have to move the shared models and business into a new python package (better if you can create a django app that encapsulates these models), in a separate directory.
Add this directory to your python path (the one that contains the package, not the package itself) and you should be able to use this code from within your projects.
The only downside to this is having to configure PYTHON_PATH in your servers or having to copy manually this package into your runtimes

Django, dynamic apps support

I am about to start a django project, where I need a base deployment, lets say just for admins initially. Later admins can add instances of my main public site.
Now, one instance will, obviously be separated by dynamic sub-domains. I need to capture sub-domains from requests, and compute accordingly. It has its own base templates, static files, etc (easiest part). It would have set of feature apps (common for all instances, but not the data in their models). And I am thinking of using Django1.2's multiple database support, and try to get one db per instance (* adding dynamically :( , if that is feasible, It will include dynamic db/model creations*). Or I can go for adding an instance foreign_key in all feature apps models, to separate them instant-wise.
If my instances were known prior to deployment, I would have used multiple database support easily by capturing the sub-domains and diverting my ORM calls to concerned db. But, that is not the case. Those has to be dynamic (added as need arises).
Now before I give it a try, to get solution/rid of delusions about it, I would want experts of SO to think about it. I would appreciate the suggestions, insights and of-course criticism.
I can make it community wiki, if suggested. Thanks guys.
Shouldn't you just run a separate Django instance in a each their Apache VirtuaHost? Then, you can have a Django settings file for each instance and they can each point at their proper database. This also simplifies your code because you don't need to map subdomain names to databases inside your views. As a real bonus, your code gets re-usable because it doesn't depend on your complex setup.
Ususally you can do instances of your app with the sites framework.