0

I'm making a kind of online shop. I have a base abstract class and child classes for all products.

***models.py***

class Item(models.Model):

    class Meta:
        abstract = True

class FirstItem(Item):
    pass

class SecondItem(Item):
    pass

I register all classes in admin.py

***admin.py***

class FirstItemAdmin(admin.ModelAdmin):
    inlines = []
    def foo():
      pass

class SecondItemAdmin(admin.ModelAdmin):
    inlines = []
    def foo():
      pass

admin.site.register(FirstItemAdmin)
...

How can I dynamically create new classes with different names like class ThirdItemAdmin(), class FourthItemAdmin(), class FifthItemAdmin()... in a cycle so that these classes could be registered in further code ?

admin.site.register(ThirdItemAdmin)
admin.site.register(FourthItemAdmin)
...

I tried like that

def classMaker(*args, **kwargs):
    for class in args:
        new_class_name = cls.__name__ + 'Admin'
        type(new_class_name, (admin.ModelAdmin,), {})

classMaker(FirstItem, SecondItem, ThirdItem, FourthItem, FifthItem)

and all classes are created, but they are invisible in further code. Is there a way to make it dinamycally or I'll have to make classes for all items manually ?

Denis
  • 1

0 Answers0