Search code examples
pythondjangoorganization

When to split sub-apps into new apps


I'm creating a film/tv related site: Films are referred to as 'titles', so I have a titles app to deal with the operations for those. However, titles can also have genres and cast members.

What is the best way to organize this? At the moment I have:

apps/titles (contains Title, and TitleGenre classes)
apps/titles/genres (contains Genre model class)

Is this the optimal solution? Sub-apps for titles may even be required, as for genres Would the casting for similar ones be within the title app? I'd obviously like to start the best way before I delve any deeper.


Solution

  • I'd 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 isn't so many that I would spread this into several apps. BUT for organization, keep your models and admin classes in separate files and import them in the folder's __init__.pys

    important notes:

    in your model make sure you include app_name in the inner Meta class.

    class Genre(models.Model):
        ...
        class Meta:
            app_label = _(u'films') #app's name
            ...
    

    make sure that any FKs are passed as strings not as classes (helps avoid cyclic dependencies)

    title = models.ForeignKey("films.Title")
    

    in your films/models/__init__.py import in the proper order so as not to have cyclic deps.

    from films.models.genre import Genre
    from films.models.actor import Actor
    from films.models.title import Title
    from films.models.actorrole import ActorRole
    

    in your films/admin/__init__.py register each of your admin classes

    from django.contrib import admin
    from lottery.models import Genre, Actor, Title
    from lottery.admin.actor import ActorAdmin
    
    admin.site.register(Actor, ActorAdmin)
    ...