6

Looking for a way how to assign a ModelAdmin instance to a different then a default application label, even in latest Django 1.8 .

Django project schema:

<root>/appone/models.py    # class ModelOne(django.db.models.Model)
<root>/appone/admin.py     # class ModelOneAdmin(
                           #     django.contrib.admin.ModelAdmin)
                           # admin.site.register(
                           #     ModelOne, ModelOneAdmin)

<root>/apptwo/models.py    # class ModelTwo(django.db.models.Model)
<root>/apptwo/admin.py     # class ModelTwoAdmin(
                           #     django.contrib.admin.ModelAdmin)
                           # admin.site.register(
                           #     ModelTwo, ModelTwoAdmin)

With the example above, each of the models appears in admin interface within its separate group labeled by application name.

  --- appone
        |
        +--- ModelOne

  --- apptwo
        |
        +--- ModelTwo

How to tell Django ModelTwo place under appone label ? (without altering appone application sources and its models!)

  --- appone
        |
        +--- ModelOne
        |
        +--- ModelTwo

  --- apptwo
        <empty>
David Unric
  • 7,421
  • 1
  • 37
  • 65

2 Answers2

6

Just add app_label to Meta class of ModelTwo:

class ModelTwo():
    class Meta:
        app_label = 'appone'

Warning: as noted by @guymaro86, this will change the table's DB name and cause migrations.

maxbellec
  • 16,093
  • 10
  • 36
  • 43
Domen Blenkuš
  • 2,182
  • 1
  • 21
  • 29
  • 6
    This doesn't really work since it has side effects. i.e. it also changes the table's DB name and causes migrations. – guymaor86 Aug 14 '18 at 19:22
3

Have you considered django-modeladmin-reorder

Github: https://github.com/mishbahr/django-modeladmin-reorder PyPi: https://pypi.python.org/pypi/django-modeladmin-reorder/

Disclaimer: I had very similar issues last year, where I wanted to place most used apps on top of the admin index. Could not find anything that suited my scenario, so I wrote my own :-)

mishbah
  • 5,487
  • 5
  • 25
  • 35
  • Thanks for the tip, I'll definitely take a look on that. Just curious what technique was used before I'd dig into sources. A middleware which intercepts AdminSite views and reorganizes widgets & forms from scratch ? Can't be done in a more simple way, some example to my original question ? – David Unric Apr 14 '15 at 08:02
  • I'm using a middleware to override the context. It may be possible to just use `app_label` meta to add new models to `appone`. I haven't tried it. Let me know if that works. https://docs.djangoproject.com/en/1.8/ref/models/options/#app-label – mishbah Apr 14 '15 at 08:42