Skip to content
Browse files

ProductAdmin automatically adds custom Product fields.

Thanks to Josh Cartmell.
  • Loading branch information...
1 parent 14d7866 commit 9e06c69feedaf4f82c528df073b04f3633f1e41f @AlexHill committed Sep 5, 2012
Showing with 31 additions and 0 deletions.
  1. +31 −0 cartridge/shop/admin.py
View
31 cartridge/shop/admin.py
@@ -157,6 +157,37 @@ class Media:
form = ProductAdminForm
fieldsets = product_fieldsets
+ def __init__(self, *args, **kwargs):
+ """
+ For ``Product`` subclasses that are registered with an Admin class
+ that doesn't implement fieldsets, add any extra model fields
+ to this instance's fieldsets. This mimics Django's behaviour of
+ adding all model fields when no fieldsets are defined on the
+ Admin class.
+ """
+ super(ProductAdmin, self).__init__(*args, **kwargs)
+ # Test that the fieldsets don't differ from ProductAdmin's.
+ if self.model is not Product and self.fieldsets == ProductAdmin.fieldsets:
+ # Make a copy so that we aren't modifying other Admin
+ # classes' fieldsets.
+ self.fieldsets = deepcopy(self.fieldsets)
+ # Insert each field between the publishing fields and nav
+ # fields. Do so in reverse order to retain the order of
+ # the model's fields.
+ for field in reversed(self.model._meta.fields):
+ check_fields = [f.name for f in Product._meta.fields]
+ check_fields.append("product_ptr")
+ try:
+ check_fields.extend(self.exclude)
+ except (AttributeError, TypeError):
+ pass
+ try:
+ check_fields.extend(self.form.Meta.exclude)
+ except (AttributeError, TypeError):
+ pass
+ if field.name not in check_fields and field.editable:
+ self.fieldsets[0][1]["fields"].insert(3, field.name)
+
def save_model(self, request, obj, form, change):
"""
Store the product object for creating variations in save_formset.

0 comments on commit 9e06c69

Please sign in to comment.
Something went wrong with that request. Please try again.