In a Django project, I have a few apps and one model-class will be created in one app's models.py and imported to another apps' models.py. then I run migrate for each app per one schema in Postgres, there is no error reported.
But the issue is the shared model will be migrated to all the schemas because (I assume this is the reason) I imported this model in each app's models.py.
Anyone knows How to avoid or solve this problem. Because I would like keep the model only stay in his original schema.
Or can I delete this model directly from each schema it appears?...without any side effects.
Please help, Thanks!
I found a better answer: Installing PostgreSQL Extension to all schemas.
should see this before.
Accidentally found the solution, just use 'public' schema for the app where models supposed to import to other apps.
Just for someone who experience the same situation.
Related
Given the following architecture of django projects apps and database schemas:
There is a django app which I want to share between several django projects.
Each project has it's own postgresql schema in behind. All schemas live in the same postgresql database.
One project is owner of the apps data, this project is responsible to run migrations and the data should live in its schema.
All other projects may access the data from the other projects schema, because they have a proper postgresql search path set.
We already use this concept with an app that has all models set to unmanaged, which works. But database changes always needs to be done manually. I'd like to benefit from django migrations and therefore I want my models either managed or unmanaged.
Do you think app config is good place to change the models meta? Any other suggestions and approaches on how to solve the requirement are also welcome.
The in the comments mentioned setting MIGRATION_MODULES works very well. In those projects which should only access the data and which should not be responsible for the migrations you can easily disable the migrations like that:
MIGRATION_MODULES = {
'yoursharedapp': None
}
Note that you need to configure the postgresql role used in the those projects with proper grants and search path.
I'm creating a bus booking website using Django. I'm stuck at what the database tables should look like. So far I've created the displayed tables, I'm very poor with databases so please mention foreign keys and primary keys when answering. Also, I'm stuck on how to actually book seats in Django, like what will be the code in the background and what other packages should I use. Thanks.
Table- Route:
Columns - route_id (primary key),
location_from,
location_to,
route_name,
Table - Bus:
Columns - Bus_id(primary key), type_of_bus,
bus_registration,
capacity,
bus_number,
route (foreign key)
Also I'm using the default sqlite database that comes with django. Is it good enough to build this kind of website or do I need to change? This website is just a project and will never go into production phase.
I'd highly recommend writing your database schema in Python classes (as Django models) and then using ./manage.py makemigrations ./manage.py migrate, which will look at your model code and create the corresponding database schema. This portion of the django tutorial could be helpful to you.
The python code corresponding to your example for a route would look something like:
from django.db import models
class Route(models.model):
location_from=models.CharField()
location_to=models.CharField()
route_name=models.CharField()
This is a much easier way to write a database schema, especially if you're not so hot with databases.
If your app will never be in production, SQLite is fine - it's very similar to other database engines for toy projects.
As for what your data model should look like, I think it depends how complex you want to make the app. For a start, I think you could add a Location model, which your Route model should reference. For making reservations, you could look into django booking - I haven't used it but it comes up on django packages when you search 'booking'.
I created my database using python manage.py syncdb And I tried to add another attribute to my model called created_date My site gives error. And I deleted my db.sqlite3 file Then reorganize my model Then error went. I want to know is this correct
As the comment says migrations are the way to do this. Native migrations were only introduced in Django 1.7 but, since you're using syncdb, I'm guessing you're using an earlier version.
For earlier versions of Django you need a third-party app called South to handle migrations for you. This will then let you change your database after creation fairly painlessly in most cases.
My django site was functioning before I installed Lion and had to reinstall everything related to development. Since then, I have deleted and recreated my database, but one of my two installed apps is being ignored in syncdb. Those tables are not present in my database.
This post suggested there might be an import error. I can import the app in question using manage.py shell, so I don't think that's it.
Both apps are definitely installed (verified by debug toolbar). Any other suggestions? I'm relatively new to Django, having been mostly an iOS developer for the past couple of years.
https://docs.djangoproject.com/en/dev/ref/models/options/#app-label
If a model exists outside of the standard models.py (for instance, if the app’s models are in submodules of myapp.models), the model must define which app it is part of.
What it doesn't mention is that they also have to be imported somewhere during the model registration phase.
When using Django's built in comment package, where is the table created when the database is synced? The model doesn't seem to be in the models.py file.
I seem to be very confused trying to implement this. I found some tutorials online but nonw of them seem to help and I can't get anything to work.
Anyone have any tips? Can anyone explain how it works?
django.contrib.comments is a reusable app packaged with Django. Reusable (sometimes called 3rd party, whether or not they are) apps have their own models.py, urls.py, etc. So if you'd like to look at a model from the comments package, browse your Django source directory. On Ubuntu, that would be something like ls /usr/lib/python2.6/django/contrib/comments.
When the sync command is run, it goes through all of the apps in your INSTALLED_APPS list in your project's settings.py, and uses their models.py files to generate database schema.
Though reusable apps can be a little confusing (especially overriding templates!) they allow for your code base to be way more modular.
If you have things configured correctly you will see:
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_comments
Creating table django_comment_flags
Also make sure you have SQLite(assuming you are using sqlite) installed and have added the django.contrib.comments to your INSTALLED_APPS