A
A
Artem Afonin2016-01-13 14:15:43
Django
Artem Afonin, 2016-01-13 14:15:43

Django: Customizing an M2M model and rendering it in the admin?

Good evening, I ran into a problem with customizing the admin panel (django-suit), the bottom line is this:
there is a Product model:
- title (char: 255)
- price (deciamal: 10/2)
...
- accesories (M2M: self)
and custom model M2M - ProductAccesories:
- product (FK: Product)
- accessories (FK: Product)
- order (PosInt)
Migrations were successful, tables were created, then we go to the admin panel, and create classes for customization of the following form:

class AccessoriesAdmin(SortableTabularInline):
    model = ProductAccessories
    verbose_name = u'аксессуар'
    verbose_name_plural = u'аксессуары'
    extra = 1

class ProductAdmin(TranslatableAdmin):
    def __init__(self, *args, **kwargs):
        super(ProductAdmin, self).__init__(*args, **kwargs)
        self.save_as = True
        self.inlines = [ValAdmin, StdAdmin, FeaturesAdmin, SpecificationAdmin, AccessoriesAdmin]
        self.exclude = ('values', 'standards', 'features',
                        'accessories', 'specification_file',)
        self.fieldsets = (
            (None, {
                'fields': (
                    'title', 'price', 'number_code', 'ordering', 'product_type',
                    'desc', 'product_pdf', 'tags', 'purposes',
                    'application',  'package', 'additional', 'is_published',
                    'recommended', 'images',
                )
            }),
        )
        self.form = ProductForm
        self.filter_horizontal = ['purposes', 'standards', 'accessories',
                                  'features', 'values', 'specification_file',
                                  'images', 'tags']
        self.search_fields = ('title', 'pk')

After this action, I fell down with a trace. error:
- 'ecommerce.ProductAccessories' has more than one ForeignKey to 'ecommerce.Product'.
How to solve this? Thanks in advance for any answer!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Lebedev, 2016-01-13
@reli

If I'm correct, then yours ProductAccesoriesis logically related to the product field product. If so, try adding the AccessoriesAdminattribute to the admin class fk_name='product'. This will clearly make it clear which field Inline

N
Nerevar_soul, 2016-01-13
@Nerevar_soul

Everything is in error. From the ProductAccessories model, there are two relationships to the Product model. Without seeing all the code, I can assume that you need to specify a related_name for one of the fields with ForeignKey.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question