Seperate folder for django models - python

I am working on Django project that will utilize different apps to fulfill certain task. Since these apps will be referring to much same data to complete these task I figure it makes since to create a separate folder with the models like this:
--Project
--App1
--App2
--models
---model1.py
---model2.py
Right now I'm having trouble with Django recognizing the models as existing, every time I run a makemigrations Django does not detect that any changes have been made
I attempted to put a __init__.py file in the /models folder but this doesn't seem to do anything.

You should not seperate models in django projects dude! models.py file must be in app folder, That's why you can not migrate.

Related

Django - no migrations folder after refactoring projects and apps names

I have refactored my app and project names, but after that Django doesn't create migrations folder in my app and doesn't actually apply my models migrations.
Even after migrations (with no warning nor error) I have no tables with my objects.
Does anybody know how to force django to do those migrations?
There can multiple reasons behind it. Please check following if you are missing something.
The app must have migrations/__init__.py folder. It automatically creates but if you did code refactoring. You can miss this.
Check INSTALLED_APPS in settings.py it should have the same app name as in admin.py.

Can I put my views.py file in the project folder of django?

I am developing a project using django python server. I have created my project on django and put all my files including views.py in the project folder and I am using it without creating any app and its working fine.
Is this the right way of doing it (or) I need to create an app instead and put all my files in the project ?
This will work fine. Views can be wherever you want.
You can add the package that is your site (the one that has settings.py in it) to INSTALLED_APPS, and then a models.py in it, management commands, et cetera will also work fine.
Apps are handy when things become big and you want to split them into smaller parts.

how to perform migration for django table created without any app

I have a django project in which I have not created any app, I directly wrote models for the project in models.py file inside project folder.
But whenever I perform
python manage makemigrations
it says : No changes detected
so when I perform
python manage migrate
it says : No migrations to apply.
So is there any different way to perform migrations if you are directly writing your models without creating any App for django project
You should add your app to django INSTALLED_APPS settings, if not, then, you dont have an django app.
migration on django is an app basis.
migrations are created and run on a per-app basis, https://docs.djangoproject.com/en/1.8/topics/migrations/
so you should create an app and add it to INSTALLED_APPS as levi suggest. However, it is possible if you're kind a want to only use one app to manage all the models for your db under models.py, BUT it's messy.
i recommend to use the migration on app basis.

South cannot find my models

I'm having trouble trying to add south to an existing django project. Here is my situation:
My project is called alerts. Here is my folder structure:
djangoprojects/
alerts/
manage.py
alerts/
__init__.py
urls.py
views.py
models.py
wsgi.py
settings.py
common/
__init__.py
models/
__init__.py
models.py
The file alerts/alerts/models.py has the following content:
from common.models.models import *
In other words, the only thing that alerts/alerts/models.py does is import the "real" models from alerts/common/models/models.py, which has a large number of models that were created from a past project.
I also have a MYSQL database that I'll call 'existing_database'. This is the database that corresponded to the models on my past project, which ran on another machine. On that other machine I used mysqldump to get the sql file for this database and copied it over to my new machine. Then I used mysql directly to create existing_database on my new machine. So now the database existing_database should match the models in alerts/common/models/models.py.
So next I went through the documentation to try to add south to the alerts project. This is what I did:
I added 'south' and 'alerts' to the INSTALLED_APPS inside alerts/alerts/settings.py. I'm not sure why I have to add 'alerts' to settings.py inside the alerts project - you'd think the fact that the settings file was in the project folder would be enough. But it complained if I didn't add it explicitly, so I did.
I cd-ed to djangoprojects/alerts and ran ./manage.py syncdb. It said it synced some stuff, including south and alerts
Then I ran ./manage.py convert_to_south alerts. This is where I ran into problems. It said:
This application has no models; this command is for applications that already have models syncdb'd.
Make some models, and then use ./manage.py schemamigration alerts --initial instead.
So I tried running ./manage.py schemamigration alerts --initial. It created an initial migration but that migration files has almost nothing in it. All it has is a class Migration. None of my real models are anywhere to be found.
To see if it can recognize any new models, I went to common/models/models.py and added a dummy model. Then I ran ./manage.py schemamigration alerts --auto and the response I got back was "Nothing seems to have changed". So it apperas it still knows nothing about my real models.
Ao at this point I'm totally confused. Like I said, I have an existing database with real data, and I have an existing set of models code that should exactly represent that actual table structure. I just want to start using this in my alerts project and I need south because I will be adding and changing models.
How do I accomplish this?

I'm a bit confused about running django tests

So I followed the instructions on the site here:
https://docs.djangoproject.com/en/1.5/topics/testing/overview/
but what confuses me is the portion that describes the scope of tests when running. It says:
By default, this will run every test in every application in INSTALLED_APPS. If you only want to run tests for a particular application, add the application name to the command line.
For example, if your INSTALLED_APPS contains 'myproject.polls' and 'myproject.animals', you can run the myproject.animals unit tests alone with this command:
What confuses me is that the directory structure for the site is laid out like so
myproject/
manage.py
mysite/
__init__.py
settings.py
urls.py
views.py
models.py
wsgi.py
So I don't really have any smaller apps. I essentially just have 1 big app which is the site. There are a number of apps that are in my INSTALLED_APPS variable but I just want to run the test on mysite. How would I go about doing that?
Or, would I have to:
Move the entire site to its own app, laying out a directory structure like this and add that app to INSTALLED_APPS
myproject/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
mysiteapp/
views.py
models.py
Also, in general would that be a better structure for my django project?
The Django Testing suite basically will test your one big site as is, which I believe is what you want.
Others structure their Django site as an aggregate of smaller apps. Like a sub app for authentication, or one for a particular feature with different requirements, or just having a bunch of components connected together so that you can reuse parts of your past projects.
In those cases, someone might only want to test one of those components and not the whole thing. For example, if you have a working site and you add in an app from a past project and everything breaks, you would want to focus your tests on that app. This is what the warning is about. Meaning that if you only want to test a sub app then you should specify it.
For your case, testing everything works because your using only one app.
HTH

Categories

Resources