I am working on a django app. I understood django creates some tables for users, permissions, groups in database. In that database, already another django app is running. so, already it has default tables. Now, I need to create other app in same database. But I think it creates conflict on users,groups,permissions. So, I want to create all tables with some other names.
Updated
I looked at django-table-prefix which doesn't work with 1.8+ .I am using django 1.10
My question is
How to create/migrate django app with different table names for users,groups, permissions or is there any other way?
Sorry for my silly question. Thanks
If you have an existing Django project, which creates the default tables, you can just add a new **app* to the project. The app can be dependent upon those tables or not, depending on the functionality of the app. Here's the section on Django's website that discusses projects versus apps: https://docs.djangoproject.com/en/1.10/intro/tutorial01/#creating-the-polls-app.
Advanced Use: Here's the section on how to write reusable apps. https://docs.djangoproject.com/en/1.10/intro/reusable-apps/
I hope this helps.
Related
I have one django project that serves as an API and contains a database and multiple apps with models, database migrations and so on.
I want to have a custom admin interface as well as django-admin which are only accessible via the intranet. Is this possible within the same django project while the other apps are accessible from outside the intranet? And if not, is it possible to have two django projects. One which serves as the API containing the database, models and migrations. And another one that contains just the django-admin and my custom admin interface app that can access the databse and models from the other project?
Your question consists of two parts.
How to share a database between Django Projects? You just need to pass the same database credentials (HOST, DB_NAME, USERNAME, and PASSWORD) to connect to a same database
How to share models? I'll describe two options here.
Creating a Django App to contain your models(Recommended)
You can create a Django app to contain your shared models. This tutorial will explain how to do that.
https://realpython.com/installable-django-app/
Then, you just need to install your app in your Django projects.
Copy Pasting your model code.
You can easily copy and paste your model codes into different projects, but syncing between them would be a problem and is not recommended.
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).
When working with databases with Django, it automatically creates 12 default tables in the database.
I understand that I need dango_session for storing the session and probably django_site. But why do I need the others?
In PHP I used to store users in my own custom tables. Shouldn't I do that anymore?
The tables are created because you have django.contrib.auth, django.contrib.sessions, and so on in your INSTALLED_APPS setting. You shouldn't delete the tables if the apps are installed, as Django will expect them to exist.
None of the contrib apps are required to run Django. However, I highly recommend that you use the Django auth and sessions app instead of writing your own. For example, if you use the auth app, you don't need to worry about how to hash passwords, and there are lots of helpful views included to log users in, reset passwords and so on.
I want to use Pinax for a small project , but I am confused because I don't if can extend/change the behavior and functional of the provided applications .
Is there any documentation for extending the behavior of the bundled applications ?
example: in registration application ,I want to add custom fields but I am not able to find proper documentation on how to achieve it..( mainly for those which need db changes )
Thanks !
Yes, you can extend the behaviour of the built-in applications. If you are using the pinax basic setup with user accounts and profiles, you will have to add the extra fields you want in apps/profiles/models.py. For a list of field types, see here: https://docs.djangoproject.com/en/1.3/ref/models/fields/
This will create the necessary db fields for you when you run manage.py syncdb. If you have already sync'd the db, however, you will have to manually add the db columns. If you don't have any data you care about in that table, you can always just drop the table and it will recreate it. Django doesn't modify db tables once they are created, even if you change the model.
You will also have to modify the signup form to include these new fields and point your urls.py to the new signup form you created. Copy the form from the site-packages/pinax directory to your project. Don't modify them directly.
If you haven't already, you should check out the Django tutorial here: https://docs.djangoproject.com/en/1.3/intro/tutorial01/
This will give you a good idea of how Django apps are put together and how the different pieces interact, so you can do a better job customizing Pinax to your liking. Make sure you know what models.py, urls.py, views.py, and the templates are doing.
Django: If I added new tables to database, how can I query them?
Do I need to create the relevant models first? Or django creates it by itself?
More specifically, I installed another django app, it created several database tables in database, and now I want to get some specific data from them? What are the correct approaches? Thank you very much!
I suppose another django app has all model files needed to access those tables, you should just try importing those packages and use this app's models.
Django doen't follow convention over configuration philosophy. you have to explicitly create the backing model for the table and in the meta tell it about the table name...