Moving Django models into their own module
I’m a neat freak when programming. Those who have seen my house might find this to be a bit of a shock, but it’s true. I’ve been using Django for a while to develop content-focused websites, but there’s been a nagging issue that’s bugged me: I hate having lots of classes in one file. I realize this is a pet peeve, and has no major issue around it other than my own preferences, but there it is.
The places this has shown up more than anywhere are in models and views. For models, it’s normal to have all your models for an application in a models.py file, and this works great when you have one or two. When you have potentially dozens, it becomes insane. So, in order to reduce my pain, I figured I’d just move them into their own files under a models module subdirectory, stuff a few from foo import * lines in the init and poof…
Bzzzzzzzzzzzzzt. Thank you for playing.
It turns out this naive approach is not really functional. Thanks to Magnus- on #django, I got a pointer to a totally undocumented requirement. It seems you’ve got to add a definition to the inner Meta class of your model definition. What’s this magical variable? app_label Don’t go looking for it in the documentation—which is otherwise excellent—as its never mentioned that I can find. You need to set it to a string which is the application module name. So if you’ve got myproject.myapp.models.foo, then you’d set app_label = 'myapp' and Bob’s your uncle. Here’s a dumb example:
from django.db import modelsclass Bar(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
class Meta: app_label = 'myapp'
Hope this helps someone when they’re searching for it, as I found zippo in Google when I did what I thought were the obvious searches.
This entry was posted at 3:41 pm on 19 January 2009 and is filed under Python. You can follow any responses to this entry through the post-specific RSS 2.0 feed.
[...] Placing Django Models In Separate Files Posted January 20, 2009 Filed under: web development | Tags: django | Chris Petrilli has made a very useful post on placing Django models into separate files. [...]
I think part of the documentation issue is that anything covered in the official docs automatically gets covered by the API stability policy, and this is something that hasn’t been finalized (there’s work ongoing to develop a cleaner way to do this, most likely by providing an abstraction over the idea of a “Django application” since that would solve several things in one fell swoop).
I wonder if perhaps there might be some “convention over configuration” that works toward this working. For me, at least, modules make infinitely more sense than dumping everything in a single file. For a tiny application, that’s not a major issue, but it quickly gets out of hand.
I didn’t see a ticket on this, is there somewhere to track?
The ticket to watch is #3591: http://code.djangoproject.com/ticket/3591
It started out as a request for something better/more unique than app_label (since it’s possible to end up with multiple applications that have the same automatic app_label), but that quickly develops into the need to support a few other use cases:
- A way to specify the human-readable name of the application (and mark it for translation).
- A way to have multiple instances of the same application (usually the admin) in use simultaneously, possibly with different configuration for each one.
- A way to cleanly “know” what application a given model belongs to, even if the models are split across multiple files.
The ticket itself hasn’t seen a whole lot of activity in a while, but there’ve been discussions on the dev list, on IRC, and in person at sprints and conferences dedicated to working it out. For the moment there’s no clear consensus on the right API, but it’s something I’d like to see get more attention for, say, Django 1.2 later this year.
Both comments and pings are currently closed.
“Thanks to Magnus-”
I think you mean Magus- ?