Django inline with PolymorphicParentModelAdmin

34 Views Asked by At

i have a problem with my CreationInline, i have this code in models.py:

class Creation(PolymorphicModel):
    """Model representing a creation."""
    original_title = models.CharField('Título original', max_length=10000, null=True, blank=True,)
    subtitles = models.TextField('Subtítulos, capítulos y entregas', null=True, blank=True,)
    authorship = models.TextField('Autoría', null=True, blank=True,)
    publication_year =  models.IntegerField("Año de publicación",default=2023, choices=((i,i) for i in range(1930, 2024)))
    synopsis = models.TextField('Sinopsis', null=True, blank=True,)
    country = models.ForeignKey(Country, on_delete=models.SET_NULL, null=True, blank=True,)
    keywords = models.ManyToManyField(KeyWord, blank=True)
    product = models.OneToOneField("Product", on_delete=models.CASCADE, related_name="product_creation")
    def __str__(self):
        return self.product.title
class MediaCreation(Creation):
    original_language = models.ForeignKey(Language, on_delete=models.SET_NULL,blank=True, null=True)
    IMDb = models.CharField(max_length=10000,blank=True, null=True)
    commertial_editions = models.CharField('Ediciones comerciales', max_length=10000,blank=True, null=True)
class Movie(MediaCreation):
    """Model representing a Movie."""
    remastering = models.TextField('Reedición',  null=True, blank=True)
    direction = models.TextField('Dirección',  null=True, blank=True)
    producer = models.TextField('Producción', null=True, blank=True)
    genre = models.ManyToManyField(Genre, limit_choices_to={"type": "Movie"})

there are other models that inherit from Creation, such us Novel, Comin, etc. My code in admin.py is:

class CreationInline(NestedStackedInline):
    model=Creation
class CreationParentAdmin(PolymorphicParentModelAdmin):
    """Parent admin class for Creation model."""
    base_model = Creation
    child_models = (Movie, Musica, TVSerie, Videogame, Theatre)
@admin.register(Creation)
class CreationAdmin(CreationParentAdmin, DisableAddButtonModelAdmin):
    """Admin class for Creation model."""
    base_model = Creation
    readonly_fields = ('product',)
    child_models = (Movie, Musica, TVSerie, Videogame, Theatre, Novel, Comic, BoardGame)
    list_filter = (PolymorphicChildModelFilter, )
    list_display = ('get_product_title', 'original_title', 'authorship', 'publication_year')
    def get_product_title(self, obj):
        return obj.product.title  # Obtiene el título del producto asociado a la creación

    get_product_title.short_description = 'Product Title'  # Establece el nombre de la columna en el list_display
@admin.register(Movie)
class MovieAdmin(HiddenModelAdmin, DisableAddButtonModelAdmin):
    def get_form(self, request, obj=None, **kwargs):    # Just added this override
        form = super(MovieAdmin, self).get_form(request, obj, **kwargs)
        form.base_fields['genre'].widget.can_add_related = False
        return form
    list_display = ('original_title', 'authorship')
    inlines = [AwardInline, OtherLanguageTitleInline, OtherCountriesDebutInline]
class ProductAdmin(NestedModelAdmin):
    list_display = ('title', 'last_modified', 'build_id')
    search_fields=('title',)
    inlines = [ParatextInline, HipotextInline, BibliographyInline, MedialTransferInline, MetatextInline, CreationInline]

And my problem is that in my view of add and change a product, in the creation iunlne i see the info to add only a creation, and i want to see a selector of the type of creation i am going to create, movie, comic, novel, and when i select one it open a window to create this type of creation. Someone can help me?

my problem is that in my view of add and change a product, in the creation iunlne i see the info to add only a creation, and i want to see a selector of the type of creation i am going to create, movie, comic, novel, and when i select one it open a window to create this type of creation. Someone can help me?

0

There are 0 best solutions below