When to split a child app into a new app
I’m creating a movie/TV related website: movies are called “titles”, so I have a title app to handle the operation of these contents. However, titles can also have genres and actors.
What is the best way to organize? Currently I have:
apps/titles (contains Title, and TitleGenre classes)
apps/titles/genres (contains Genre model class)
Is this the optimal solution? A sub-application of titles
may even be necessary, as for whether genres converts
similar types in the title application? Before diving in, I obviously want to start in the best possible way.
Solution
I’ll do something along these lines:
MyProject/
|- films/
|- __init__.py
|- urls.py
|- models/
|- __init__.py
|- genre.py
|- actor.py
|- title.py
|- actorrole.py //M2M through with actors and their roles in specific titles
|- admin/
|- __init__.py
|- genre.py
|- actor.py
|- title.py
|- views/
|- __init__.py
|- someview.py
|- myproject/
|- __init__.py
|- urls.py
|- wsgi.py
|- settings/
|- __init__.py
|- production.py
|- staging.py
|- local.py
3 or 4 models is not so much that I would spread it across several applications. But for organization, save the model and management classes in separate files and import them into the __init__.py
of the folder
Important:
In your model, make sure that you include app_name in your internal meta class.
class Genre(models. Model):
...
class Meta:
app_label = _(u'films') #app's name
...
Ensure that all FKs are passed as strings and not as classes (helps avoid circular dependencies).
title = models. ForeignKey("films. Title")
Import in the correct order in your films/models/__init__.py
to avoid circular dependencies.
from films.models.genre import Genre
from films.models.actor import Actor
from films.models.title import Title
from films.models.actorrole import ActorRole
Register each of your management classes in your films/admin/__init__.py
from django.contrib import admin
from lottery.models import Genre, Actor, Title
from lottery.admin.actor import ActorAdmin
admin.site.register(Actor, ActorAdmin)
...