F
F
Friend2020-09-27 11:48:17
Django
Friend, 2020-09-27 11:48:17

How to display inlines OneToOneField with multiple links in Django Admin?

class BaseProduct(models.Model):
    name = models.CharField(max_length=200)


class Image(models.Model):
    base_product = models.ForeignKey(BaseProduct, related_name='images_product', on_delete=models.CASCADE)
    image = models.ImageField()
    

class Product(models.Model):
    descriptions = models.TextField(blank=True)
    base_product = models.OneToOneField(BaseProduct, on_delete=models.CASCADE, related_name='product')


In Django Admin inlines does not display OneToOneField, ForeignKey is not needed on product.
Through from django_reverse_admin import ReverseModelAdmin, you can only display BaseProduct, but the Image connected to it cannot be displayed. The output goes through the Product, since there are many of them and they are different, all are connected to the BaseProduct.

class ProductAdmin(ReverseModelAdmin):
    model = Product
    inline_reverse = ['base_product']
    inline_type = 'stacked'

admin.site.register(Product, ProductAdmin)


How can output in Django Admin through Product BaseProduct and Image?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question