Login

Tag "custom-model-field"

Snippet List

Encrypted Custom Model Field

Custom model field to support two ways encryption. The key is provided in the app settings.py file. It should be at least 32 chars. ** usage ** Assuming that you placed the new code into fields.py from app.fields import EncyptedField class Account(models.Model): password = EncryptedField()

  • custom-model-field
  • encrypted
Read More

ByteSplitterField

When you want to save integers to the db, you usually have the choice between 16-, 32- and 64-bit Integers (also 8- and 24-bit for MySQL). If that doesn't fit your needs and you want to use your db-memory more efficient, this field might be handy to you. Imagine you have 3 numbers, but need only 10 bit to encode each (i.e. from 0 to 1000). Instead of creating 3 smallint-fields (48 bit), you can create one 'ByteSplitterField' which implements 3 'subfields' and automatically encodes them inside a 32 bit integer. You don't have to take care how each 10-bit chunk is encoded into the 32-bit integer, it's all handled by the field (see also field's description). Additionally, the Field offers opportunity to use decimal_places for each of your subfields. These are 'binary decimal places', meaning the integer-content is automatically divided by 2, 4, 8, etc. when you fetch the value from the field. You can also specify how values are rounded ('round' parameter) and what happens when you try to save a value out of range ('overflow' parameter) Not implemented (maybe in the future if I should need it sometime): * signed values. All values are positive right now! * real (10-based) decimal places (actually you could probably directly use DecimalFields here) * further space optimization, i.e. saving into CharField that's length can be chosen byte-wise

  • model
  • db
  • database
  • field
  • custom
  • custom-model-field
  • IntegerField
  • multibit-field
  • model-field
Read More

Custom model field for Frame or Box

**Purpose** We often need to store x,y, width and height for a model, we can store all these values in same field by having custom field. **How to Use** Save this code in *customfields.py* and in your model >*from customfields import FrameField* >*class MyModel(models,Model)* >* frame = FrameField()* And in your in views, you can use as follows >*model = MyModel.objects.get(1)* >*print model.frame.x, model.frame.y, model.frame.width, model.frame.height*

  • field
  • custom-model-field
  • frame
Read More

3 snippets posted so far.