I'm a bit confused about running django tests - python

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

Related

Seperate folder for django models

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.

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.

What is the purpose of adding to INSTALLED_APPS in Django?

Most documentation simply tells you to add the name of each of your apps to the INSTALLED_APPS array in your Django project's settings. What is the benefit/purpose of this? What different functionality will I get if I create 2 apps, but only include the name of one in my INSTALLED_APPS array?
Django uses INSTALLED_APPS as a list of all of the places to look for models, management commands, tests, and other utilities.
If you made two apps (say myapp and myuninstalledapp), but only one was listed in INSTALLED_APPS, you'd notice the following behavior:
The models contained in myuninstalledapp/models.py would never trigger migration changes (or generate initial migrations). You wouldn't be able to interact with them on the database level either because their tables will have never been created.
Static files listed within myapp/static/ would be discovered as part of collectstatic or the test server's staticfiles serving, but myuninstalledapp/static files wouldn't be.
Tests within myapp/tests.py would run but myuninstalledapp/tests.py wouldn't.
Management commands listed in myuninstalledapp/management/commands/ wouldn't be discovered.
So really, you're welcome to have folders within your Django project that aren't installed apps (you can even create them with python manage.py startapp) but just know that certain auto-discovery Django utilities won't work for that application.

Run some initialization code when Django (1.9) loads

I saw that from 1.7 there is https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready, however this is per each app.
What I am looking for is a way to call some initialization code when Django is loaded, regardless of which apps it contains.
Many people suggest adding the code to wsgi.py, urls.py and settings.py, however all these methods are 'dirty'.
My scenario is that I have several apps that could be part of the Django deployment, and I would like any subset to have this init code.
I could write a special common app, but I assume there must be a Django way of providing init code.
In newer versions of Django, you generally have a project directory containing the global, app-independent files like settings.py and wsgi.py. You can add global initialization code to __init__.py in that directory.
Be advised that this code is run very early (even before the settings are loaded), so some parts of the Django API are not available yet.

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.

Categories

Resources