Django models not showing up in DB after syncdb

I have a folder with models in which there are several models in files that are already in the database. I just added another file / model, but it is not added to the database when syncdb starts. I tried manage.py validate and it works fine. I also run the code, and it only fails when it tries to save the "table does not exist".

The original structure was like this:
/ models
- __ init __ .py
- file1.py
- file2.py

and __ init __ .py looked like:

from file1 import File1Model from file2 import File2Model 

I added file3.py
/ models
- __ init __ .py
- file1.py
- file2.py
- file3.py

and modified by __ init __ .py

 from file1 import File1Model from file2 import File2Model from file3 import File3Model 

And the contents of the file3 (names can be changed to protect the innocent, but except that it is the exact file):
UPDATE: just tried adding the primary key, since the id field may have been messing around with the automatically added integer primary key identifier. Also tried several options, but did not play dice.

 from django.db import models from django.contrib.auth.models import User class File3Model(models.Model): user = models.OneToOneField(User) token = models.CharField(max_length=255, blank=False, null=False) id = models.CharField(primary_key=True, max_length=255) class Admin: pass class Meta: app_label = 'coolabel' def __unicode__(self): return self.user.username @staticmethod def getinstance(user, token, id): try: instance = File3Model.objects.get(pk=id) if instance.token != token: instance.token = token instance.save() return instance except: pass instance = File3Model() instance.user = user instance.token = token instance.id = id instance.save() return instance 

So, in this example, File1Model and File2Model are already in the database and remain in the database after syncdb. However, File3Model is not added even after running syncdb again. Is there any way to find out why the new model is not being added?

+6
source share
4 answers

Boom!

I used the new app_label for the new model, but it should be the same in the model group.

Other label models were "mediocrelabel" and my new model was labeled "coolabel". I replaced the new model label with "mediocrelabel" and now they are added to the database correctly.

Thanks for the help guys!

0
source

If you define a model outside models.py, you need to set the app_label attribute in the Meta model class.

Edit: app_label should reference the application in the INSTALLED_APPS setting. Probably, this should coincide with the name of the application in which the model catalog is located, if you have no reason for this. It seems to have been your problem.

 class File3Model(models.Model): foo = models.CharField(...) ... class Meta: app_label = "my_app" 

Note that syncdb will never delete tables from db. Other tables were probably created using syncdb before models.py was replaced with a directory structure.

+9
source

setting app_label in my application solves my problem.

+1
source

Why did you separate the models and have a folder with samples instead of placing models in models.py ?

In my own project, about 10 models live in models.py , and I'm fine with it.

You can also try manage.py syncdb --all .

And I think that it is better to save all models in one file and import them as from my_app.models import model_name , instead of having to import the necessary model into models/__init__.py . By the way, you avoid many problems and long- some_model imports and do not care about where some_model lives among models/*.py files.

Thanks,

The sultan

0
source

Source: https://habr.com/ru/post/917108/


All Articles