Creating migrations for a reusable Django app - django

I am writing a reusable Django app and having problems creating migrations.
I have looked at this question, and I'm still confused. I have the following sort of directory structure:
django-mycleverapp/
django-mycleverapp/django_mycleverapp/
django-mycleverapp/django_mycleverapp/__init__.py
django-mycleverapp/django_mycleverapp/apps.py
django-mycleverapp/django_mycleverapp/models.py
django-mycleverapp/django_mycleverapp/urls.py
django-mycleverapp/django_mycleverapp/views.py
django-mycleverapp/example/
django-mycleverapp/example/manage.py
django-mycleverapp/example/example/
django-mycleverapp/example/example/__init__.py
django-mycleverapp/example/example/settings.py
django-mycleverapp/example/example/urls.py
django-mycleverapp/setup.py
As you can see, the directory "django_mycleverapp" contains my reusable app and the directory "example" contains a test project.
I include the models of "django_mycleverapp" in the INSTALLED_APPS section of the settings for "example".
However, running python ~/example/manage.py makemigrations django_mycleverapp doesn't build any migrations.
Any suggestions?
How am I meant to have a test project that will build migrations in "/django-mycleverapp/django_mycleverapp/migrations"?

Your app should be in the directory of your project. Your directory hierarchy should look like this.
django-mycleverapp/
django-mycleverapp/example/
django-mycleverapp/example/django_mycleverapp/
django-mycleverapp/example/django_mycleverapp/__init__.py
django-mycleverapp/example/django_mycleverapp/apps.py
django-mycleverapp/example/django_mycleverapp/models.py
django-mycleverapp/example/django_mycleverapp/urls.py
django-mycleverapp/example/django_mycleverapp/views.py
django-mycleverapp/example/manage.py
django-mycleverapp/example/example/
django-mycleverapp/example/example/__init__.py
django-mycleverapp/example/example/settings.py
django-mycleverapp/example/example/urls.py
django-mycleverapp/example/setup.py
If you do not want your app to be part of your "example" project, but rather want it to be separated and used by your project "example", you'll have to install it in your project using pip (this requires to have a setup.py at the root of your app).
For instance if you have published your app on a git repository "https://gitlab.com/myuser/myproject.git", you can add to our requirements.txt:
git+https://gitlab.com/myuser/myproject.git#v1.0#egg=myapp_name
If you don't have your app published on a git repository yet, you can add the absolute path to your app to you requirements.txt:
/path/to/django-mycleverapp/django_mycleverapp/
Don't forget to work in a virtualenv when you use pip.

Related

Installing Pinax, where is my deployment folder?

I'm trying to set up Pinax and I'm new to everything involved (Django, Pinax, webservers, etc). I'm following http://pinax.readthedocs.org/en/latest/gettingstarted.html
When I generate a project using:
(mysite-env)$ pinax-admin setup_project -b basic mysite
The directory structure I get is:
apps __init__.py manage.py settings.pyc urls.py
dev.db __init__.pyc requirements static urls.pyc
fixtures locale settings.py templates wsgi.py
Which as far as I can tell is missing the deployment folder (when you compare to the directory structure shown here : http://pinax.readthedocs.org/en/latest/starterprojects.html). It doesn't seem to be effecting anything yet, but it makes me nervous. What is going on and is the fact I'm missing the deployment folder going to cause problems in the future?
I'm running Ubuntu and using python 2.7. I had the same behaviour with Windows 7, python 2.6
Thanks!
The new Django versions have made the old pinax pretty much useless. Now Django supports project templates and Pinax is separated into several smaller projects regarding starter projects (such as pinax-project-account) and apps (such as django-user-account).
The current way to use pinax is to choose a starter project, and then running something like:
$ django-admin.py startproject --template=https://github.com/pinax/pinax-project-account/zipball/master <project_name>
and then install requirements:
$ pip install -r requirements.txt
This will create a new Django project using the starter-project as a template, which already includes a few apps (like django-user-account) and templates (with bootstrap!). The project is ready to run, and already includes a bunch of functionality (like user registration, login and management).
Also, Django has changed the project directory structure a bit, so now it doesn't really look like that anymore.

Separate Git repository for multiple Django sites

I am using Django as development framework for my current Project (say Project A).
I want to create a new application (say Project B) that has different views as Project A. But this new Project B is going to use most of the existing backend modules and models from current Project A.
Such that my Current Project (Project A) is hosted on URL http://www.site-one.com and and New Project B will be hosted on http://www.site-two.com. With respect to this, I have one doubt and a question that I want to ask:
Doubt: Is my choice of using "Django sites framework" for this is correct?
Question: Is there any way to keep two Django sites (A and B) in different Git Repo and still access the models and other modules from Current Site A?
I would suggest for shared functionality create separate django apps with a structure resembling something like the following. (I'm assuming you've got your projects in virtualenvs)
appname/
__init__.py
models.py
views.py
templates/
base.html
Store this app in a git repository of it's own.
then inside the requirements.txt for each of your projects this functionality is needed in add a line like the following, assuming you're using bitbucket:
-e git+https://yourusername#bitbucket.org/appname/appname.git#egg=appname
install
pip install -r requirements.txt
then in your settings.py add myappname to your INSTALLED_APPLICATIONS

How can i install external pinax projects?

I try to deal with poor documentation of Pinax.
I found this project
https://github.com/pinax/pinax-multiblog-project
what i want is that install it. I normally install a project called account but here i have to install from git . How can i do that in Pinax
Edit:
It turns out the new way of using projects is to just copy the folder and rename it.
Usage instructions:
So to use the multiblog project you would do
Copy the multiblog inside the cloned repo to a new location and rename it to the name you would like to use for your project. Then install the requirements via pip and follow the rest of the steps for setting up a django project
OSX/Linux:
cd ~/src
git clone https://github.com/pinax/pinax-multiblog-project
cp -r ~/src/pinax-project-admin ~/Sites/new_project
cd ~/Sites/new_project"
pip install -r requirements.txt
python manage.py syncdb
I too have been trying to accomplish the same thing. So far I found this commit
https://github.com/nrb/pinax/blob/476d2398c48cc444eb2338c12090f0cebad46961/docs/starterprojects.txt
Relevant section on begins on line 160 near the end
External Starter Projects
=========================
The Pinax ``setup_project`` command can also use starter projects built by third parties.
These can either be plain directory structures, or they may be a git/hg pip editable.
To install a starter project from an external source, simply pass the file path or git/hg
URL to the ``-b`` option::
pinax-admin setup_project -b git+git://github.com/user/project.git#egg=project my_new_project
However all the pinax projects I have come across don't seem to include an egg to use
eg:
https://github.com/pinax/pinax-multiblog-project
https://github.com/pinax/pinax-project-account
I need to use Django 1.4 for my project but the included account base project in both Pinax 0.9a2 and 0.9b1-dev10 use Django 1.3.
I am guessing the external project integration is something we will have to wait for in the Pinax 1.0 release.

Cannot start any django app

I am a newbie at Django and everytime I try to run
python panel/manage.py startapp %app% (panel is my project) it gives me the error:
Error: '%app%' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Am I doing something wrong?
Surely companies or contacts or stats is not the name of an existing Python module?
This is a fun one - your project and your app need to have different names. You probably created a project, then tried to startapp with the same name.
I was confused as well, until I realized that a Django project is a container for applications; this sequence makes it a bit clearer:
# first create a Project (container).
django-admin.py startproject Project
# create multiple apps
cd Project
python manage.py startapp polls
python manage.py startapp hello
...
Perhaps you need to
cd panel
python manage.py startapp yourappname
I'm not sure running the command from a directory above your project will work properly.
I had the same issue because I was trying to "restart" my app after carrying out changes, but startapp is meant to be used once to create a new app. To view changes, syncronize app with DB with python manage.py migrate and restart the server with python manage.py runserver instead.
From the django-admin docs
(manage.py does essentially the same thing as django-admin)
startapp <app_label> [destination]
django-admin startapp
Creates a Django app directory structure for
the given app name in the current directory or the given destination.
By default the directory created contains a models.py file and other
app template files. (See the source for more details.) If only the app
name is given, the app directory will be created in the current
working directory.
If the optional destination is provided, Django will use that existing
directory rather than creating a new one. You can use ‘.’ to denote
the current working directory.
For example:
django-admin startapp myapp /Users/jezdez/Code/myapp
This message is displayed if you run "startapp" twice with the same app name. As pointed out above by the OP it doesn't reload the app, it creates one.
You should choose different names for your project and app in Codes:
django-admin startproject **my_project**
python manage.py startapp **my_app**
You need to create the directory before using the commands. Suppose you want a polls app inside apps folder.
mkdir apps apps/polls
python manage.py startapp polls apps/polls
I guess maybe you have already created the app's dir in panel dir manually. The command 'startapp' is to create an app automatically. If you already have one there, it fails.
I reproduced the issue and there's actually something not working as I expected.
I wonder if we stumbled upon a Django's bug, or a limitation that I don't understand.
Having a project called "project" and an empty folder app/newapp
…I tried:
python manage.py startapp newapp apps/newapp
It returns:
CommandError: 'newapp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
But if I target ANY other route in which the last folder is not called the same name as the app I'm starting, it works.
So I ended up doing:
python manage.py startapp newapp apps/main
Using Django 2.1.3.
if you want to make an empty directory that will contain your new app
project-dir
└── blog
├── __init__.py
├── ...
├── blog-ext #this empty dir that will contain the new app
└── views.py
so instead of typing :
python manage.py newapp blog/blog-ext
it should be :
django-admin startapp newapp blog/blog-ext
Try classic "mysite" or "myproject". You can delete it anytime you want, so if it will accepted, then all your privious ideas conflict with Python modules.
Edit: I tried all your ideas, there was no error for me. So, if you installed support libraries or modules for django, then some of them can contains such names.
this error is because of the name conflicts between the app name and project name.you had given same name for your app and project .your project and app need to be different name .if you had given the same name the above mentioned error will occur .
understand the difference between app and project
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 simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.
first create the project.
then create the app.
NOTE: name for app and project should be different
first create a project with projectname
django-admin.py startproject Projectname .
Then create app with appname. (to create your app make sure you are in the same directory manage.py and type this command)
python manage.py startapp Appname
It's the process how I got my doubt clear.
First, I created a directory inside my project directory and put __init__.py, models.py, admin.py, apps.py & views.py.
Then I ran python manage.py runserver & It work well.
Then as suggested on that page I used startapp command. I got this error :
CommandError: 'ucportal' conflicts with the name of an existing Python
module and cannot be used as an app name. Please try another name.
After that I deleted that directory and ran startapp command with same name and it worked fine.
So 'startapp' command is to create an app automatically. If you already have one there, it fails.
Answer given by #DAG worked for me.
I ran into this issue while trying to set up a Wagtail project.
Before creating the app, I had created and activated a virtualenv (using virtualenvwrapper) with the same name: $APPNAME. When I then ran wagtail start $APPNAME, Django looks for naming conflics in the $PYTHONPATH which in this instance points to /Users/User/.virtualenvs.
Naturally, this results in a conflict as /Users/User/.virtualenvs/$APPNAME already exists.
None of these answers helped me. In the end I ended up creating an app with a different name and then just renaming the directory to the app name I wanted all along. Note that you also will need to change the class name in apps.py to match your app name.
Just Simply Use This command
for Django Project Creation
python -m django startproject name_of_django_Project
for Django App Creation
python -m django startapp App_name
I had the same issue when working with wagtail cms. I got this error even there is no such a created app. This occurs when there is an app already that has the same name you need to create inside the site-packages directory.
Once you get this error, you need to check the following directory,
C:\Users\{user}\AppData\Local\Programs\Python\Python38-32\Lib\site-packages
If there is a package with the name same you want to create then you need to remove that package. Also make sure to check that package is important or not before deleting.
The application directory should be created first.
Example: apps/practice
The command appears to be duplicated, but it is correct.
Example: python manage.py startapp practice ./apps1/practice

Django: the role of the project name

I am thinking of stating a new Django project and I have to choose a project name now, so I can type:
djangoadmin startproject <something>
This raises doubts, I'm not sure of the name, and I think that I might want to change it in the future. So, I have two questions:
What role does project name play in the project code and deployment?
What steps do I need to take to change my project's name?
Thank you!
Main project name is used as a base for your namespace.
By default, you will have in settings.py line: "ROOT_URLCONF = 'something.urls'".
To change a project name, you need to change every single import that is referring to it.
Of course you can always use modules without 'something' prefix, then you must ensure that there will be no name/namespace conflict between modules. I'm using this option, because I can have same code in a few copies without additional hassle.
Late to this party, but for future reference this may help somebody. I've just had to change a project name because it clashed with the name of a third-part app. It's easier to change a project name than an app name! (Django 1.11)
Folder structure
project/
manage.py
project/
settings.py
urls.py
wsgi.py
venv/
If using a virtual environment in venv, generate an up to date requirements.txt with pip freeze
Rename both project/ folders to newproject
Change project to newproject in the python files:
manage.py, look for DJANGO_SETTINGS_MODULE
settings.py, look for DJANGO_SETTINGS_MODULE and WSGI_APPLICATION, and a comment. You can and should leave the name of the database and the database user unchanged, assuming you want to keep the data.
urls.py, in a triple-quoted string
wsgi.py, DJANGO_SETTINGS_MODULE plus a comment
If using a virtual environment, you need to recreate it. I renamed venv to old.venv, then virtualenv-3 venv, then use pip install and the requirements file you generated at 1. Trash old.venv when sure the new one is working AOK.
/path/to/project will also feature in system config files such as /etc/nginx and a .service file for gunicorn, which will need changing to /path/to/newproject.
Restart the server and test. Should be working.
Now you can add an app called project to your INSTALLED_APPS!
I now know that it's a good idea to call in-house Django projects ${my_org_name}_something, or similar, so they won't clash with third-party apps.