views:

101

answers:

4

Talking about Django 1.1.1. I thought at one time (0.96) the kinds of things put inside of the admin.py file were part of an inner class of the model.

There's a certain beauty in having all of this in one place. But I don't know if this change was out of necessity. Any compelling reasons one way or the other?

+1  A: 

They took away that particular magic, but there is nothing to keep you from putting your admin.ModelAdmin subclass right after the models.Model subclass itself. I prefer keeping them together myself because it's less likely I'll forget to add a field to the list to show in the admin display.

Peter Rowell
A: 

Perhaps it would not be a good idea to have this stuff in the models anyway, since it would be excess information when using the site as a non-admin? (For performance reasons)

Fragsworth
A: 

There isn't any way to express the admin options inside the model definition as an inner class in the latest version. But there is no reason why you can't put your ModelAdmin class right after your Model class in your models.py file. You can then just call your admin.site.register() right after your definition.

You may run into a problem with the register() being called more than once for a model, which will generate an error. models.py should only get loaded once though so this should work. If not, you can definitely declare your ModelAdmin class in models.py, and then put all your register() calls in admin.py.

A couple reasons that I can think of to put it them in admin.py are:

  • Convention -- seems like this is becoming a best practice.
  • Decoupling -- the admin definitions don't really have much to do with the model.
  • Cleanness -- probably no need to fill up your models.py file with stuff you aren't going to change much.

But if your models.py file isn't going to be very long I can see the attraction of having them right next to each other.

sheats
I agree ... sort of. Although it's true that contrib.admin is not in the core like models.Model (well, it's not in django.core, but you know what I mean), many of us consider admin to be one of the crown Jewels of Django. And the fact is that I often fiddle with the field ordering and grouping for the admin change page. When I add a new field to the model it feels ... not quite right to have to go edit some other file to make that show in admin. Of course, I still yearn for curly braces, so maybe I'm just a dinosaur.
Peter Rowell
I tried putting an Admin inner class to my model. Immediately ran into trouble with my inline definitions. Since the model I want to 'inline' is defined later *in the same file* it crashes with a NameError, 'is not defined'. This might be the straw...
Off Rhoden
@Off Rhoden: You would need to put your admin definition after your model class. It does need to be defined first before you can reference it.
sheats
A: 

use class Admin inside your models as u use class Meta and then define what u want

ha22109