class OrganizationMaterialVariantPriceForm(ModelForm):
def __init_subclass__(cls, **kwargs):
if cls.__name__ == OrganizationMaterialVariantPriceForm.__name__:
for color in Color.objects.all():
field_name = f'color_{color.slug}'
cls.declared_fields[field_name] = IntegerField(
label=color.name,
required=False,
widget=UnfoldAdminIntegerFieldWidget()
)
class Meta:
model = OrganizationMaterialVariantPrice
fields = []
class OrganizationMaterialVariantPriceInline(TabularInline):
model = OrganizationMaterialVariantPrice
extra = 0
form = OrganizationMaterialVariantPriceForm
fields = ['material']
def get_fields(self, request, obj=None):
fields = super().get_fields(request, obj) + [
f'color_{color.slug}' for color in Color.objects.all()
]
return fields
-
-
Save dannluciano/031bef4d92241c1f39ab1e6ed73787e5 to your computer and use it in GitHub Desktop.
How to Create a Dynamic ModelForm in Django Admin to Display Dynamic Columns
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment