[Django]-Django admin with multiple inlines of the same model

4๐Ÿ‘

A better solution than inlines with custom queryset is to use Proxy Models with custom manager which adds the default filtering. With that solution you can easily re-use those models outside of the admin:

# models.py

class HotelRoomManager(models.Manager):
    def get_query_set(self):
        return super(HotelRoomManager, self).get_query_set().filter(prefix='room')

class HotelRoom(ItemProvided):
    """ Only ItemProvided with prefix=room """
    objects = HotelRoomManager()

    class Meta:
        proxy = True

class HotelServiceManager(models.Manager):
    def get_query_set(self):
        return super(HotelServiceManager, self).get_query_set().filter(prefix='service')

class HotelService(ItemProvided):
    """ Only ItemProvided with prefix=service"""
    objects = HotelServiceManager()

    class Meta:
        proxy = True


#### admin.py

class HotelRoomInline(admin.StackedInline):
    model = HotelRoom

class HotelServiceInline(admin.StackedInline):
    model = HotelService

class HotelAdmin(admin.ModelAdmin):
    inlines = [
           HotelRoomInline,
           HotelServiceInline,
           ]
๐Ÿ‘คMariusz Jamro

Leave a comment