Skip to content

Instantly share code, notes, and snippets.

@lovevn
Forked from specialunderwear/meta.py
Created November 2, 2016 09:08
Show Gist options
  • Save lovevn/febcb2dab48c85beba421aa27600a7be to your computer and use it in GitHub Desktop.
Save lovevn/febcb2dab48c85beba421aa27600a7be to your computer and use it in GitHub Desktop.
Override model fields of an abstract model baseclass in django, by removing them from the abstract model.
def AbstractClassWithoutFieldsNamed(cls, *excl):
"""
Removes unwanted fields from abstract base classes.
Usage::
>>> from oscar.apps.address.abstract_models import AbstractBillingAddress
>>> from koe.meta import AbstractClassWithoutFieldsNamed as without
>>> class BillingAddress(without(AbstractBillingAddress, 'phone_number')):
... pass
"""
if cls._meta.abstract:
remove_fields = [f for f in cls._meta.local_fields if f.name in excl]
for f in remove_fields:
cls._meta.local_fields.remove(f)
return cls
else:
raise Exception("Not an abstract model")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment