Hi friends ,
As we know enum data type is used to reduce memory in database . we can pass string as input and can get output as string ---but in really it will strore in DB as 1 digit number starts from 0 with increase of 1 .
but django does not provide us default django field so we need to make our own CUSTOM ENUM FIELD . you can go through djnago docs to know about it Django DOCS
but for enum you just make youe own class
# Class which describe Custom Field of name Enum
class EnumField(models.Field):
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 104
super(EnumField, self).__init__(*args, **kwargs)
if not self.choices:
raise AttributeError('EnumField requires `choices` attribute.')
def db_type(self):
return "enum(%s)" % ','.join("'%s'" % k for (k, _) in self.choices)
# choices means data in string form to display in mysql and django admin interface
STATE =(('C' , 'C'),
('P', 'P),
('F, 'F'),
('F', 'F'),
('R', 'R'),)
As we know enum data type is used to reduce memory in database . we can pass string as input and can get output as string ---but in really it will strore in DB as 1 digit number starts from 0 with increase of 1 .
but django does not provide us default django field so we need to make our own CUSTOM ENUM FIELD . you can go through djnago docs to know about it Django DOCS
but for enum you just make youe own class
# Class which describe Custom Field of name Enum
class EnumField(models.Field):
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 104
super(EnumField, self).__init__(*args, **kwargs)
if not self.choices:
raise AttributeError('EnumField requires `choices` attribute.')
def db_type(self):
return "enum(%s)" % ','.join("'%s'" % k for (k, _) in self.choices)
# choices means data in string form to display in mysql and django admin interface
STATE =(('C' , 'C'),
('P', 'P),
('F, 'F'),
('F', 'F'),
('R', 'R'),)
# model for orderstate field
class Table(models.Model):
state = EnumField(choices=STATE,verbose_name ='orderstate')
now u can make your own enum field and any custom field by just edit some code in custom class
method -- >> db_type is used for making db data type here it is enum
and in __init__ --> you can customize your model and field in admin interface
Django Developer
No comments:
Post a Comment