I have two models:
- PrimaryModel. Has a choice value (Alive/Finished)
- AuxModel. Has a foreign key for the PrimaryModel and only is showed when the status of the primary model is equal to “Finished”.
# Model.py
from django.db import models
# Create your models here.
class PrimaryModel(models.Model):
class Status(models.TextChoices):
ALIVE = 'Alive', 'Alive'
FINISHED = 'Finished', 'Finished'
status = models.CharField(choices=Status.choices, default=Status.ALIVE, max_length=15)
class AuxModel(models.Model):
primary = models.ForeignKey(PrimaryModel, on_delete=models.CASCADE)
campo = models.CharField(max_length=15)
I have decided to make the logic to show/hide the inline in the backend through get_inlines
(it sounded easy) so i make this:
# admin.py
from django.contrib import admin
from .models import AuxModel, PrimaryModel
class AuxInline(admin.StackedInline):
model = AuxModel
@admin.register(PrimaryModel)
class PrimaryAdmin(admin.ModelAdmin):
fields = ('status',)
def get_inlines(self, request, obj):
if obj and obj.status == PrimaryModel.Status.FINISHED:
return [AuxInline]
return []
This works great when i create a new object but when i intent to change the status to FINISHED, the inlines show this error and cancel the change
(Hidden field TOTAL_FORMS) This field is required.
(Hidden field INITIAL_FORMS) This field is required.
ManagementForm data is missing or has been tampered with. Missing fields: auxmodel_set-TOTAL_FORMS, auxmodel_set-INITIAL_FORMS. You may need to file a bug report if the issue persists.
This is a strange error because i only modify the get_inlines
. if it helps, I’m using Django 3.2