Create Models in other than models.py files (Django-Wiki) - python

I am using Django wiki module in my project , IN wiki module models are created in their different files.Structure are as follows.
models/
article.py
abcd.py
...
__init.py__
Now I want to add some new models into article.py but after creating models when I try to migrate them I am getting the message , Nothing seems to change. However If I change any field of existing model then migration catch the change.
My init.py files imports all models of articles.py , So I think migration must have created the models but It doesn't . so can anyone tell me where I am wrong.
Thanks

Related

How many model fields are too much in a single model django

well I have a doubt, in general you will have many models in a models.py , and that model would have many fields under it(Charfield, foreign key etc...) How many model object are too much in a single model file, for eg if I have 50 model object is it too much? Pls comment your thought on limit for the model object and what's the solution....
The "hard limit" should be very high. But to keep you project structured I'd recommend you to split them into separate apps or model files. There's no recommended number of models per file, but I personally try to stick to max 7 to keep everything organized. You can go higher if you prefer.
You can either structure like this, with separate apps:
my_django_project
manage.py
app1/
models.py
app2/
models.py
app3/
models.py
my_django_project/
settings.py
or like this within the same app:
my_django_project
manage.py
app1/
models/
__init__.py
mymodel1.py
mymodel2.py
mymodel3.py
my_django_project/
settings.py
Inside __init__.py you have to import all models like this:
# __init__.py
from .mymodel1 import *
from .mymodel2 import *
from .mymodel3 import *
Which one you choose is up to you. I prefer to have separate apps. And try to keep the in a logically separated, i.e. which models belongs in the same app?

How to "autodiscover" a different location for the admin.py file?

I think it makes sense to organize my Django apps in a different way (by protocol):
📂myapp
apps.py
models.py
utilities.py
📂html
admin.py
urls.py
views.py
📂rest
serializers.py
urls.py
views.py
📂graphql
etc
This would move the admin.py file into the html folder, but sadly autodiscover does not seem to find it anywhere else than in the myapp folder. Is there a way to point to the correct location: perhaps in apps.py? I am sure a symlink would work, but not really what I am after.
There's nothing magic about either admin.py or autodiscover. All admin.py does is run any register calls on import, and all autodiscover does is look for an admin.py file in each installed app and import it. There's nothing to stop you importing your admin.py in its custom location from somewhere else, eg the models or views files.
This was somewhat tricky, but the comment by #Joran Beasley set me in the right direction.
For admin.py to be autodiscovered it needs to be either in the top level app folder, or be imported in a file. However... not every file works.
If you do from .html import admin
into __init__.py or apps.py: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
into models.py: Results in an importerror from models into admin. I think due to the circular nature of the imports.
However the following works: from . import admin inside the urls.py. Make sure to add a comment that this import is needed for autodiscover purposes.

Renaming model breakes migration history

I have two models in two different apps:
# app1 models.py
class App1Model(models.Model):
pass
# app2 models.py
from app1.models import App1Model
class App2Model(App1Model):
pass
And I want to rename App1Model and then recreate App2Model to have explicit OneToOneField instead of magic app1model_ptr. So I create migration which deletes App2Model completely (I don't care about data because whatever reason), it runs successfully. Then I create migration in first app which renames App1Model and it runs perfect too, I check this table with new name and all it's relationships (and to it too), it's fine.
And then weird thing happens: when I run makemigrations or migrate on app2 I get an error
django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'app2.App2Model'>]
It fails while creating current project state on very first migration of app2 (0001_initial.py in app2 migrations) where this model was created first time by inheriting from App1Model with its old name.
Is there some way of fixing this? In current state App2Model already deleted, App1Model renamed and I can't do anything with migrations on app2 because of this issue.
P.S. I use Django 1.10.2
Just found solution:
Need to add last migration of app2 where I deleted App2Model as dependency to migration of app1 where I renamed App1Model so project state will be built in correct order. Actually error message itself has something related to it but I failed to catch the point:
This can happen if you are inheriting models from an app with
migrations (e.g. contrib.auth) in an app with no migrations; see
https://docs.djangoproject.com/en/1.10/topics/migrations/#dependencies
for more
I put it here for future me and for those who will suffer from similar thing.

Django Models: Where is Model class?

On documentation it has been said that each model is a python subclass of models.Model class that is inside django folder followed by db folder. When i look inside my django folder i see a db folder which was expected and inside my db folder i see models folder which contains lot of files. But I was expecting a models.py package which contains Model class. So my doubt is from where does this models.Model class come from?
This might be not a top level question but I am hopeful someone here will certainly help.
Nope it's not magic it's django , it does exist if you go to the django's source code, you can see that the Model class exists, But in that way don't you had to import models in this way??:
from django.db.models.base import Model
Yes you can do it in that way, But django does it for you in the models/__init__ file, so the only thing you have to do is:
from django.db import models
models refers to the models folder inside django.db but when you import it, it brings you all the things that are inse the models/____init__.py file, I recommend you to read this

syncdb doesn't create table models

I'm working on a web application with Django framework. I have this structure :
project
web_app
models
models.py
rwtool
settings.py
urls.py
views.py
I defined my models in models.py file. In my settings file I added this line project.web_app in INSTALLED_APPS. When I did
python mange.py syncdb
the models are not created. To create them I must add the project.web_app.models to my settings file. I have looked to others project and they don't do like this and it works. Is it correct what I'm doing?
models.py should be under your app folder (in your case "web_app").
If you want to separate your models then you have to turn models folder into a python package, in this case add an __init__.py in that folder and make sure that in the init file your models are accessible to the outside world at the path "web_app.models.ModelName".
So, solution 1 (standard Django approach):
project
web_app
models.py
Solution 2:
project
web_app
models
__init__.py
ModelOne.py
ModelTwo.py...
the __init__.py will contains something like:
from .ModelOne import ModelOne
from .ModelTwo import ModelTwo
The approach N2 can be also used for views and other stuff too ;)
First add your app to INSTALLED_APPS , and then run
python manage.py inspectdb > somefile.txt
You can get quickly check out if your database structure is matching your django models.
If python manage.py inspectdb > somefile.txt not created any structure, then make sure that your app is added to to INSTALLED_APPS.

Categories