Login

The model field subclass returns string generated from the list of choices.

Author:
I159
Posted:
November 16, 2011
Language:
Python
Version:
Not specified
Tags:
model_field
Score:
0 (after 0 ratings)

if you have multiple choice field in front and you need just a string contains chosen values this is it! Unicode encoding row related with another snippet - "Switched value typed choice field", In typical cases, you do not need to call additional methods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class MultipleTypedChoiceModelField(models.Field):
    """
    Saving value of all the checked checkboxes in the string
    """    
    def get_internal_type(self):
        return 'MultipleTypedChoiceModelField'
    
    def get_prep_value(self, value):
        # In typical cases, you do not need to call additional methods,         
        # as incoming list already contains strings. 
        unicoded_val = [val.__unicode__() for val in value]
        return ', '.join(unicoded_val)

More like this

  1. Multiple Choice model field by danielroseman 6 years, 4 months ago
  2. MultiSelectField with comma separated values (Field + FormField) by quinode 2 years, 10 months ago
  3. Preview tag for fields with choices by kmmbvnr 6 years, 11 months ago
  4. Choices by cronosa 5 years, 2 months ago
  5. Django enumeration for model field choices by martinthenext 3 years ago

Comments

Please login first before commenting.