2

How to move models to another section in the Django admin site?

In my application module models.py, I have models that are displaying in admin tool in section called "Backend". I want them to display in another section under the name "Requests".

I tried the following

class TransportationRequest(models.Model):
   ...
   class Meta:
      app_label = _('Requests')
      db_table = 'backend_transportationrequest'

It is works, but now I have issues with South as it is creating migrations to delete all of these models.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Konstantin
  • 65
  • 7

1 Answers1

4

Your current issue is that you are trying to change the app_label and db_table, which ends up changing the location of the model data within the database. By default, the database table is generated as [app_label]_[model_name] (backend_transportationrequest in your case), so when you modify both of these, South detects that the model has been removed and created again, even if this isn't actually the case.

The Django migrations framework introduced in 1.7 should have fixed this, so it detects that the model was moved (instead of deleted and created). You may need to fake a migration along the same lines as this with south, which can be done by modifying the two mgirations it generates to not actually delete and create the tables, but rename them.

Django does not currently allow you to easily do this, as the admin site expects that each application that is registered has a unique app_label. You may have luck playing with the label property of your AppConfig, but this is specifically not recommended and has been historically known to cause massive headaches.

One possibility may be to create a clone of your previous model, and only use it to register the app with the Django admin. You would need to create a proxy model with the custom app_label and db_table. If this didn't work (though it should), the other option would be to clone the model as a unmanaged model using the app_label and db_table.

Community
  • 1
  • 1
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237