64👍
✅
I don’t know about elegant, but one pragmatic way is:
import django.db.models.options as options
options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('schema',)
Obviously, this would break if Django ever added a ‘schema’ attribute of its own. But hey, it’s a thought…you could always pick an attribute name which is less likely to clash.
4👍
Not a direct answer, but I did not like the idea of adding it in every model where I need it to the options, so I did:
class MyModel(models.Model):
class Meta:
ordering = ["myfield"]
class MyPrefixMeta:
my_value = "Abc"
You could even put this to a abstract model and validate the set class properties in __init__
function or do things like adding a _myprefix_meta
property to the model. So you had your own meta class.
- [Django]-How exactly do Django content types work?
- [Django]-Update to Django 1.8 – AttributeError: django.test.TestCase has no attribute 'cls_atomics'
- [Django]-How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead."
3👍
This works for me for setting extra fields in meta for a Django model.
class Vendor(CustomModel):
def __init__(self, *args, **kwargs):
cls = self.__class__
meta = getattr(cls, '_meta', None)
setattr(meta, 'exclude_logging', ["otp", "is_otp_verified"])
super().__init__(*args, **kwargs)
This exclude_logging
field can be accessed as below.
class CustomModel(models.Model):
objects = UpdateManager()
class Meta:
abstract = True
def save(self, *args, **kwargs):
print(self._meta.exclude_logging)
I hope this solves the problem.
- [Django]-Django models: Only permit one entry in a model?
- [Django]-How to assign items inside a Model object with Django?
- [Django]-Numpy Array to base64 and back to Numpy Array – Python
Source:stackexchange.com