Answer the question
In order to leave comments, you need to log in
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')
Answer the question
In order to leave comments, you need to log in
If I'm correct, then yours ProductAccesories
is logically related to the product field product
. If so, try adding the AccessoriesAdmin
attribute to the admin class fk_name='product'
. This will clearly make it clear which field Inline
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 questionAsk a Question
731 491 924 answers to any question