Question
How can I integrate m2m fields into modelAdmin, considering that this field has through
argument and that I can’t change the DB? Or how can I use inlinetabular admin with add more button?
Description
I have Django 5 app working with legacy DB (PostgreSQL).
This DB has 3 tables described by models bellow:
class EmployeeLinkRole(models.Model):
employee = models.ForeignKey(
"Employees",
models.DO_NOTHING,
db_column="Employee_ID",
primary_key=True,
)
role = models.ForeignKey("Roles", models.DO_NOTHING, db_column="Role_ID")
class Meta:
managed = False
db_table = 'Timesheet"."Employee_Link_Role'
unique_together = (("employee", "role"),)
class Employees(models.Model):
employee_id = models.BigAutoField(
db_column="Employee_ID", primary_key=True
)
employee_name = models.CharField(
max_length=500, db_column="Employee_Name", blank=True, null=True
)
role = models.ManyToManyField(
"Roles",
through=EmployeeLinkRole,
through_fields=("employee", "role"),
)
class Meta:
managed = False
db_table = 'Timesheet"."Employees'
class Roles(models.Model):
role_id = models.AutoField(db_column="Role_ID", primary_key=True)
role_name = models.CharField(max_length=500, db_column="Role_Name")
class Meta:
managed = False
db_table = 'Timesheet"."Roles'
My admin code:
class RoleInline(admin.TabularInline):
model = Employees.role.through
@admin.register(Employees)
class EmployeesAdmin(admin.ModelAdmin):
inlines = [
RoleInline,
]
@admin.register(Roles)
class RolesAdmin(admin.ModelAdmin):
inlines = (RoleInline,)
Issue
The issue I am facing: inline model is show and work fine in both Employees and Roles models.
However I can’t make it to show “Add more” button in Employees admin.
In Employees
In Roles
Expectations
I need an “add more” button in my inlinetabular admin in both Employees and Roles admins.
What I checked
I’ve spent loads of time searching for a solution, including talking to GPT.
I’ve found out that the block with the button has “display: none” attribute.
I’ve already tried to add extra = 4
and max_num = 10
attributes to inline admin.
I’ve already tried to redefine save
and has_changed
methods of ModelAdmin class.
I’ve already tried to collectstatic
.