idlesign / django-etc

Tiny stuff for Django that won't fit into separate apps.
https://github.com/idlesign/django-etc
BSD 3-Clause "New" or "Revised" License
39 stars 1 forks source link

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

Open nikatlas opened 8 months ago

nikatlas commented 8 months ago

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

idlesign commented 8 months ago

Hi.

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

nikatlas commented 8 months ago

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

idlesign commented 8 months ago

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 commented 8 months ago

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.