-
-
Save amirrpp/29b717c8448e0ade72b3 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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