Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 945
David Unric Avatar asked Apr 13 '15 13:04

David Unric


2 Answers

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.

like image 150
Domen Blenkuš Avatar answered Sep 17 '22 14:09

Domen Blenkuš


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

like image 37
mishbah Avatar answered Sep 17 '22 14:09

mishbah