Login

Tag "field"

Snippet List

CSVField for forms

FileField that checks that the file is a valid CSV and if specified in `expected_fieldnames` checks that the fields match exactly. The widget's `accept` parameter is set to accept csv, text and excel files. **TODO**: Validate the entirety of the CSV file, not just the headers. But this should be enough for most use cases, as checking the whole file could be computationally expensive for huge files. Example usage: people = CSVField(expected_fieldnames=['First Name', 'Last Name'])

  • forms
  • csv
  • field
Read More

Filter changelist by a numeric field using a number of common value ranges

## How to use Use this [admin filter](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter) together with a numeric field to allow filtering changlist by field values range (in this case, age groups): For example, to group customers by age groups: class Customer(models.Model): # ... age = models.IntegerField() age.list_lookup_range = ( (None, _('All')), ([0, 2], '0-2'), ([2, 4], '2-4'), ([4, 18], '4-18'), ([18, 65], '18-65'), ([65, None], '65+'), )) class CustomerAdmin(admin.ModelAdmin): list_filter = [('age', ValueRangeFilter), ] ## Inspiration [This snippet](https://djangosnippets.org/snippets/587/) (for django < 1.4) inspired me to make this work for newer django versions.

  • filter
  • admin
  • field
  • range
Read More

autogenerated UUID model field

Provides UUIDField for your models. This version creates very short UUID represenation (21 chars) when the record is added eg. in admin. Generated ids are safe to be used in URLs. You can put represent it in admin as 'readonly_fields=("uuid",)'

  • model
  • field
  • uuid
Read More

Multiple emails form field

Validate form field that include email or emails separated by 'token' kwargs, by default ',' a comma. Return a list [] of email(s). Check validity of the email(s) from django EmailField regex (tested with 1.3, but normally will also work with 1.5)

  • multiple
  • email
  • validation
  • form
  • field
  • list
Read More

regex search in admin forms

Despite warning coming from django developers, I'm still using admin classes to quickly get into reverse engineering databases. One feature is missing: searching into fields thanks to a regex. One dirty solution I found is to overwrite get_search_results. But most of the code comes from django itself. If anyone has a better idea ;) **Usage:** 1. works since get_search_results is part of ModelAdmin (1.5 if I remember well) 2. Inherit your Admin class from RegexModelAdmin 3. enclose by / the field you want to regex with: `search_fields = ['/field/', ]`

  • admin
  • search
  • regex
  • field
Read More

A Lazy ModelChoiceField implementation

Sometimes we may need to generate a *ModelChoiceField* in which choices are generated at runtime, depending on the locale language. The snippet generates a *ChoiceField* based on a queryset and a specific attribute of the Model, ordering the choices by the attribute content in the locale language. **Usage example** (inside a form declaration) country = LazyModelChoiceField(sort_by='name', queryset = \ Country.objects.all, empty_label=_('All countries'), label=_('Country')) Based on lsbardel LazyChoiceField implementation (snippet 1767)

  • forms
  • model
  • field
  • ModelChoiceField
Read More

CompressedTextField for Django 1.4+

This snippet *updates* http://www.djangosnippets.org/snippets/383/ and http://www.djangosnippets.org/snippets/1495/ for Django 1.4+, and adds support for sqlite3 and south. Original snippet text: A CompressedTextField to transparently save data gzipped in the database and uncompress at retrieval.

  • text
  • model
  • field
  • compressed
  • gzip
  • south
Read More

Unique field inline formset

This method will return an inline formset class that validates values across the given field are unique among all forms. For instance: ApprovedUserFormSet = inlineformset_factory(Request, ApprovedUser, formset=unique_field_formset('email'), form=ApprovedUserForm) Will make sure all ApprovedUser objects created for the Request have unique "email" fields.

  • field
  • unique
  • formset
  • inlineformset
Read More

UKPhoneNumberField GB v3 (improved)

Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890". Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service')) Can reject multiple number types so you can tune the form input to accept only landline or only mobile, or whatever combination you want. Corrects the errors found in http://djangosnippets.org/snippets/1207/ and adds extra functionality and detail to the code found at http://djangosnippets.org/snippets/2809/ In particular, this version rejects individual invalid area codes and caters for area codes with mixed-length numbering in fine-grained detail. **Uses info from:** [here](http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers)

  • form
  • field
  • uk
  • localflavor
  • form_field
  • model_field
  • telephone
  • gb
  • area-code
Read More

UKPhoneNumberField GB v2

Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890" Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service')) Corrects the errors found in http://djangosnippets.org/snippets/1207/

  • form
  • field
  • uk
  • localflavor
  • form_field
  • model_field
  • telephone
  • gb
  • area-code
Read More

136 snippets posted so far.