Skip to content

Commit

Permalink
ProductAdmin automatically adds custom Product fields.
Browse files Browse the repository at this point in the history
Thanks to Josh Cartmell.
  • Loading branch information
AlexHill committed Sep 5, 2012
1 parent 14d7866 commit 9e06c69
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cartridge/shop/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9e06c69

Please sign in to comment.