4 from django
.db
import models
7 class MyWrapper(object):
8 def __init__(self
, value
):
12 return "<%s: %s>" % (self
.__class
__.__name
__, self
.value
)
14 def __unicode__(self
):
17 def __eq__(self
, other
):
18 if isinstance(other
, self
.__class
__):
19 return self
.value
== other
.value
20 return self
.value
== other
22 class MyAutoField(models
.CharField
):
23 __metaclass__
= models
.SubfieldBase
25 def __init__(self
, *args
, **kwargs
):
26 kwargs
['max_length'] = 10
27 super(MyAutoField
, self
).__init
__(*args
, **kwargs
)
29 def pre_save(self
, instance
, add
):
30 value
= getattr(instance
, self
.attname
, None)
32 value
= MyWrapper(''.join(random
.sample(string
.lowercase
, 10)))
33 setattr(instance
, self
.attname
, value
)
36 def to_python(self
, value
):
39 if not isinstance(value
, MyWrapper
):
40 value
= MyWrapper(value
)
43 def get_db_prep_save(self
, value
, connection
):
46 if isinstance(value
, MyWrapper
):
50 def get_db_prep_value(self
, value
, connection
, prepared
=False):
53 if isinstance(value
, MyWrapper
):