djangosnippets.org: Latest snippets tagged with 'field'http://djangosnippets.org/tags/field/2018-01-17T18:38:07.719306-06:00FieldField Auto delete file on change or delete
2018-01-17T18:38:07.719306-06:00hieunv495http://djangosnippets.org/snippets/10638/<p>FileField delete file on delete or update</p>
Freely redistributableCSVField for forms
2016-12-22T12:35:09.399590-06:00AgustinLadohttp://djangosnippets.org/snippets/10596/<p>FileField that checks that the file is a valid CSV and if specified in <code>expected_fieldnames</code> checks that the fields match exactly.</p>
<p>The widget's <code>accept</code> parameter is set to accept csv, text and excel files.</p>
<p><strong>TODO</strong>: Validate the entirety of the CSV file, not just the headers. But this should …</p>
Freely redistributableAutomatically round Django's DecimalField according to the max_digits and decimal_places attributes
2016-02-08T07:39:25.735738-06:00alexpirinehttp://djangosnippets.org/snippets/10554/<p>This snippet allows you to use a new field type, RoundedDecimalField, that will automatically round any value affected to it according to the max_digits and decimal_places attributes. You will no longer receive validation errors if you use a float or a decimal with too many decimal digits.</p>
Freely redistributableFilter changelist by a numeric field using a number of common value ranges
2015-09-03T09:16:55.105197-05:00asfaltboyhttp://djangosnippets.org/snippets/10527/<h2>How to use</h2>
<p>Use this <a href="https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter">admin filter</a> together with a numeric field to allow filtering changlist by field
values range (in this case, age groups):</p>
<p>For example, to group customers by age groups:</p>
<pre><code>class Customer(models.Model):
# ...
age = models.IntegerField()
age.list_lookup_range = (
(None, _('All')),
([0, 2], '0-2'),
([2, 4], …</code></pre>
Freely redistributableModify properties on fields inherited from an abstract superclass
2014-06-29T07:08:37.346959-05:00andybakhttp://djangosnippets.org/snippets/3079/<p>See http://stackoverflow.com/questions/927729/how-to-override-the-verbose-name-of-a-superclass-model-field-in-django/24475838#24475838</p>
Freely redistributableautogenerated UUID model field
2014-03-06T12:02:47.514647-06:00kedarhttp://djangosnippets.org/snippets/3034/<p>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.</p>
<p>You can put represent it in admin as </p>
<p>'readonly_fields=("uuid",)'</p>
Freely redistributableMultiple emails form field
2013-12-17T14:48:42.881052-06:00matrixhttp://djangosnippets.org/snippets/3012/<p>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)</p>
Freely redistributableregex search in admin forms
2013-11-24T14:47:30.132742-06:00mbeehttp://djangosnippets.org/snippets/3005/<p>Despite warning coming from django developers, I'm still using admin classes to quickly get into reverse engineering databases.</p>
<p>One feature is missing: searching into fields thanks to a regex.</p>
<p>One dirty solution I found is to overwrite get_search_results. But most of the code comes from django itself.</p>
<p>If anyone has …</p>
Freely redistributableA Lazy ModelChoiceField implementation
2013-07-16T12:13:39.949163-05:00alecdoticohttp://djangosnippets.org/snippets/2973/<p>Sometimes we may need to generate a <em>ModelChoiceField</em> in which choices are generated at runtime, depending on the locale language. The snippet generates a <em>ChoiceField</em> based on a queryset and a specific attribute of the Model, ordering the choices by the attribute content in the locale language.</p>
<p><strong>Usage example</strong> (inside …</p>
Freely redistributableInsert field attributes in template
2013-05-10T05:53:45.345901-05:00Sergio@Keyhttp://djangosnippets.org/snippets/2919/<p>Usage:</p>
<p><code>{%formfield form.description rows="3"%}</code></p>
Freely redistributableCompressedTextField for Django 1.4+
2013-02-15T10:32:25.505165-06:00devhuluhttp://djangosnippets.org/snippets/2893/<p>This snippet <em>updates</em> 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.</p>
Freely redistributableUnique field inline formset
2012-09-13T15:42:50.612941-05:00dcwatsonhttp://djangosnippets.org/snippets/2816/<p>This method will return an inline formset class that validates values across the given field are unique among all forms. For instance:</p>
<pre><code>ApprovedUserFormSet = inlineformset_factory(Request, ApprovedUser, formset=unique_field_formset('email'), form=ApprovedUserForm)
</code></pre>
<p>Will make sure all ApprovedUser objects created for the Request have unique "email" fields.</p>
Freely redistributable"Ukrainian telephone number" model and form fields v2 UAPhoneNumberField (improved)
2012-09-06T05:23:34.036046-05:00g1smdhttp://djangosnippets.org/snippets/2811/<p>Validate Ukraine telephone numbers in popular formats:</p>
<pre><code>+380 XX XXX-XX-XX
0XX-XXX-XX-XX
(0XX) XXX-XX-XX
</code></pre>
<p>This snippet fixes the errors found in
http://djangosnippets.org/snippets/2579/</p>
Freely redistributableUKPhoneNumberField GB v3 (improved)
2012-09-06T03:36:07.525793-05:00g1smdhttp://djangosnippets.org/snippets/2810/<p>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".</p>
<p>Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))</p>
<p>Can reject multiple number types …</p>
Freely redistributableUKPhoneNumberField GB v2
2012-09-06T03:17:29.403905-05:00g1smdhttp://djangosnippets.org/snippets/2809/<p>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"</p>
<p>Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))</p>
<p>Corrects the errors found in …</p>
Freely redistributable