django-etc icon indicating copy to clipboard operation
django-etc copied to clipboard

CustomModelPage: `makemigrations` without app name creates a migration in django.contrib.admin.migrations

Open nikatlas opened this issue 1 year ago • 4 comments

Hello!

I want to utilise the CustomModelPage to do some custom action requiring user input without a model.

Issue

Running python manage.py makemigrations a new migration is generated in the admin package.

import json

from django.db import models

from etc.admin import CustomModelPage, admins
import app.django_tasks as tasks


class TaskManagerAdmin(admins.CustomPageModelAdmin):
    fields = ('args', 'kwargs', 'sync')


class TaskManagerPage(CustomModelPage):
    title = 'Task Manager'  # set page title

    # Define some fields.
    args = models.CharField('args', max_length=10000, blank=True, default='')
    kwargs = models.TextField('kwargs', max_length=100000, blank=True, default='')
    sync = models.BooleanField('Run synchronously', default=True)

    admin_cls = TaskManagerAdmin  # set admin class for this page

    def save(self):
            ...

TaskManagerPage.register()

Expectation

I would expect no model to be required for this.

Environment

Django v3.2 Python v3.8.10

nikatlas avatar Feb 07 '24 15:02 nikatlas

Hi.

Thank you for the report. What version of Django do you use?

idlesign avatar Feb 08 '24 01:02 idlesign

Django v3.2 Python v3.8.10

@idlesign How do you normally work with the CustomModelPage without getting a migration? An abstract model is not allowed to be registered in the admin

nikatlas avatar Feb 08 '24 09:02 nikatlas

How do you normally work with the CustomModelPage without getting a migration? An abstract model is not allowed to be registered in the admin

Interestingly enough I see no migrations for Custom Model Page inherited classes. Although the code is similar to yours (and as in docs). Yet my setup currently is Django 4.2, I'll try to reproduce the behaviour to find out the reason.

idlesign avatar Feb 09 '24 03:02 idlesign

Heh, I missed that you run makemigrations without an application name.

This indeed will create a new migration file in django.contrib.admin.migrations.

So the workaround is to specify the application name, e.g. makemigrations app .

It'll require a futher investigation to prevent migration creation.

idlesign avatar Feb 09 '24 03:02 idlesign