4

I have a Django project on a Centos VPS.

I created some models and debugged them so they validate and give no errors. I have them in a "models" folder in my myapp and have added each model to the init file in this directory, for example:

from category import Category

I added the app to settings.py INSTALLED_APPS and ran:

Python manage.py syncdb

It appeared to work fine and added all tables apart from the ones for my app.

I then installed South and added that to INSTALLED_APPS and, tried syncdb again and ran:

Python manage.py schemamigration myapp --initial

It generated the file correctly but nothing was in it (none of the tables my models).

An example file in "models" folder (usertype.py)

from django.db import models

class UserType(models.Model):
    usertype_id = models.PositiveIntegerField(primary_key=True)
    description = models.CharField(max_length=100)
    is_admin = models.BooleanField()
    is_moderator = models.BooleanField()

class Meta:
    app_label = 'myapp'

Any ideas what could be going wrong here and why I can't get anything to detect my models?

user3227889
  • 41
  • 1
  • 1
  • 5

2 Answers2

10

Run the following commands

python manage.py makemigrations yourappname

python manage.py migrate

Note it works on django 1.7 version for me.

Rafiq
  • 2,000
  • 2
  • 21
  • 27
8

you're misunderstanding the process of working with south. South isn't just another application, it's a managing tool. Your app needs to be a South application from the begining or converted to one. That being said, the process is like so:

  1. Add South to INSTALLED_APPS
  2. run syncdb for the first time
  3. Add your app to INSTALLED_APPS*
  4. run the south initialization command:

    python manage.py schemamigration myapp --initial
    
  5. migrate:

    python manage.py migrate
    

If you want to convert a project:

  1. Run syncdb after adding south
  2. run:

    manage.py convert_to_south myapp

And use south from now on to manage your migrations.

*p.s. - you can add both south and your own app at the same time, if you keep in mind to put south before your own apps. That's because django reads INSTALLED_APPS in order - it runs syncdb on all apps, but after installing south it won't install the rest and instead tell you to use the south commands to handle those

edit

I misled you. Since you put so much emphasis on the south thing I didn't realize the problem was you were trying to use models as a directory module instead of a normal file. This is a recognized problem in django, and the workaround is actually exactly as you though in the first place:

say this is your structure:

project/
       myapp/
            models/
                  __init__.py
                  bar.py

you need bar.py to look like this:

from django.db import models

class Foo(models.Model):
    # fields...

    class Meta:
        app_label = 'myapp' #you need this!

and __init__.py needs to look like this:

from bar import Foo

Make sure it looks like this and it will work.

UPDATE 18/08/2014

The ticket has changed to wontfix, because apparently the bigger issue with the app_label has been fixed. Huzza!

yuvi
  • 18,155
  • 8
  • 56
  • 93
  • I am using --initial because it's a new app with no models in db. I tried "manage.py convert_to_south myapp" just to see what happened and 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 myapp --initial instead." As mentioned, I can run --initial and it executes as expected aside from the fact the generated file has nothing in it, it does not detect any of the models. These are new models none of which are in db. – user3227889 Jan 23 '14 at 15:19
  • 1. The way you described it in your post is not the way I described it 2. Can you share models.py? Also any additional information you think is relevant. I don't think I see the entire problem yet – yuvi Jan 23 '14 at 15:21
  • I have now removed the references from `__init__.py` in "models" folder as per your recommendation. Thanks for that, though it didn't change my issue as stated with no models being detected. – user3227889 Jan 23 '14 at 15:26
  • I didn't expect it to change anything, I just mentioned it. – yuvi Jan 23 '14 at 15:28
  • Cool, I have now added an example of one of the models to the description of the issue above. – user3227889 Jan 23 '14 at 15:31
  • Ok - looking at your model everything seems fine. Now- here's the thing. If you haven't ran `syncdb` on south yet, the application isn't a south application. Therefore, it **should most definitly** create tables for your models.py. So the problem isn't related to south probably, but something else. Are you sure you put the app name in your installed_apps properly? Also- why are you setting your app_label? You don't need to do that unless you want it to be different – yuvi Jan 23 '14 at 15:55
  • Yes syncdb (without South) does not work either as mentioned. In INSTALLED_APPS I just have 'myapp' where myapp/models is the relative path (from where manage.py and settings.py is) to my models. I thought app_label was needed, but perhaps not then so I shall remove it. – user3227889 Jan 23 '14 at 17:12
  • it isn't needed, but even so, I created a new test app and everything worked fine with it. I have no idea what you're doing wrong, but it doesn't have anything to do with south – yuvi Jan 23 '14 at 17:14
  • This maybe because I haven't installed Django properly. I'm using the system version of Python as I failed to get Django on my altinstall of a later Python version. However when I try to make a new project `django-admin.py startproject mytestapp` I get `-bash: django-admin.py: command not found`. I installed Django with `yum install Django` – user3227889 Jan 23 '14 at 18:16
  • That's probably the problem. Try a fresh install inside an isolated virtualenv, that might do the trick – yuvi Jan 23 '14 at 19:41
  • I managed to make a fresh project (it just needed `django-admin startproject mytestapp` - no .py). I have the same issue with the fresh project – user3227889 Jan 23 '14 at 20:08
  • Django. Try re-installing django – yuvi Jan 23 '14 at 20:17
  • I just did a pip install of Django 1.6.1 which replaced 1.4.8 for the system python version I'm using (2.6). This hasn't resolved the issue. – user3227889 Jan 23 '14 at 20:58
  • I really can't what's the problem without more information. I can tell you that it isn't related to south, and probably related to your setup. I suggest you use a virtualenv which creates an isolated version of python and pip. Install everything from scratch there, maybe there's something wrong with your system site-packages... – yuvi Jan 23 '14 at 21:07
  • I've followed some instructions online to try and do this (install latest Python fresh with altinstall and make virtualenv). I got a few errors along the way but proceeded. Sadly trying to `sudo python virtualenvwrapper-4.2/setup.py` just got `distutils.errors.DistutilsFileError: The setup.cfg file /usr/local/bin/Python-3.3.3/setup.cfg does not exist.` – user3227889 Jan 23 '14 at 23:05
  • I spun up a new VPS and did everything from scratch but I still have the same problem!!! – user3227889 Jan 24 '14 at 16:14
  • I have no idea. Sorry. – yuvi Jan 24 '14 at 16:37
  • OK so the issue is that it wont pick up models when they are in a 'models' folder! any idea why? – user3227889 Jan 24 '14 at 16:47
  • I've just given up and kept all models is models.py cos this is too much hasslet - thanks for help anyways. – user3227889 Jan 24 '14 at 17:57
  • I'm sorry, I have misled you. I didn't notice the problem was with trying to use models as a folder. You're original thought was right, though you probably did *something* wrong there. The edit to my answer now includes a tested working solution. Make sure it looks like that and everything will work – yuvi Jan 25 '14 at 09:13