David Unric
David Unric

Reputation: 7719

Django model under other app label in admin?

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>

Upvotes: 6

Views: 5331

Answers (2)

Domen Blenkuš
Domen Blenkuš

Reputation: 2242

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.

Upvotes: 6

mishbah
mishbah

Reputation: 5597

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 :-)

Upvotes: 3

Related Questions