djangosnippets.org: Latest snippets tagged with 'modelform'http://djangosnippets.org/tags/modelform/2016-01-06T19:37:09.149179-06:00delete object from table in form
2016-01-06T19:37:09.149179-06:00beckastarhttp://djangosnippets.org/snippets/10548/<p>I had a difficult time understanding how to delete an item from a table within a template, using a modelform. I couldn't find a good example, so I wanted to post the code that ultimately worked. </p>
Freely redistributableReusable form template with generic view
2015-12-02T08:02:40.154798-06:00roldandvghttp://djangosnippets.org/snippets/10541/<p>If you require lots of forms in your project and do not want to be creating an extended template for each one I propose this solution.</p>
<p>Classes in the html correspond to bootstrap, you can work without them if you do not use bootstrap.</p>
Freely redistributableview ModelForm
2013-04-18T08:57:36.246741-05:00hrdcdrhttp://djangosnippets.org/snippets/2910/<p>qwe</p>
Freely redistributableCustom Form Example: Forms for Bootstrap Html - CSS Toolkit
2011-12-16T04:29:38.036646-06:00fatiheriklihttp://djangosnippets.org/snippets/2631/<p>Improved for BootStrap
details: http://twitter.github.com/bootstrap/</p>
<p>Using:</p>
<p>class AnyForm(forms.Form, CustomForm):
pass</p>
<p>Template:</p>
<p><form>
{{ form.render_errors }}
{{ form.as_div }}
</form></p>
Freely redistributableModelForm Class saving m2m
2011-10-11T08:29:09.453455-05:00ckarrie2http://djangosnippets.org/snippets/2568/<p><strong>Your model:</strong></p>
<pre><code>class TicketItem(models.Model):
hours = models.DecimalField(decimal_places=2, max_digits=6)
day = models.DateField()
order = models.ForeignKey(Order)
tickets = models.ManyToManyField(Ticket)
</code></pre>
<p>Now you want to auto save m2m fields in your forms.TicketItemCreateForm:</p>
<ol>
<li>inherit from m2mForm-Class</li>
<li>define m2m_field(s)</li>
</ol>
<p><strong>Example:</strong></p>
<pre><code>class TicketItemCreateForm(m2mForm):
m2m_field = 'tickets'
class Meta:
model = models.TicketItem
</code></pre>
Freely redistributableAdd a CSS class to every field indicating what kind of field it is
2010-07-06T13:48:00.194102-05:00ramenhttp://djangosnippets.org/snippets/2097/<p>The admin site uses a CSS class for each kind of field so that they can be styled easily. This snippet gives ModelForms a similar feature.</p>
<p>See also: <a href="http://stackoverflow.com/questions/660929/how-do-you-change-the-default-widget-for-all-django-date-fields-in-a-modelform/661171#661171">James Bennett's explanation of formfield_callback</a></p>
Freely redistributableUse JQuery Calendar in ModelForm
2010-07-01T12:39:33.350435-05:00Induanehttp://djangosnippets.org/snippets/2088/<p>The important code really is just setting up the base site to use jquery and then using the javascript function to show the calendar on a widget with the .vDateField class set. The DateField modeltype automatically gets the css class .vDateField when using ModelForms.</p>
Freely redistributableTinyMCE Widget
2009-08-28T14:56:18.952333-05:00sementehttp://djangosnippets.org/snippets/1705/<p>Widget for TinyMCE 3.2.6, a WYSIWYG HTML editor for <code>textarea</code>.</p>
<p><strong>Note:</strong></p>
<blockquote>
<p>This snippet uses the TinyMCE package thats contains special jQuery build of TinyMCE and a jQuery integration plugin. Anyway, is easily to adapt to standard package.</p>
</blockquote>
<p>Usage example:</p>
<pre><code>from django.contrib.flatpages.admin import FlatpageForm
class MyFlatPageForm(FlatpageForm):
content = forms.CharField(widget=TinyMCEEditor())
</code></pre>
<p><a href="http://tinymce.moxiecode.com/download.php">TinyMCE …</a></p>
Freely redistributableMaking a django inline (model) formset really tabular
2009-04-15T20:00:46.499263-05:00fnlhttp://djangosnippets.org/snippets/1442/<p>As there is no straight way to re-produce the real tabular inline formsets you get in django-admin, here is how this template has to look like if you do it form your own formsets generated from formset factories.</p>
Freely redistributableAdd model form field meta data in a DRY way
2009-03-17T06:47:41.928674-05:00jediehttp://djangosnippets.org/snippets/1375/<p>Change field meta data or add a html attribute to a ModelForm field in a DRY way.</p>
Freely redistributableLazy options on ModelForm fields - like setting a ModelChoiceField queryset from the view
2008-12-31T06:38:43.871329-06:00jpichttp://djangosnippets.org/snippets/1272/<p>Example view code:</p>
<pre><code>lazy_field_options = {
'field_name_that_is_m2m': {
'queryset': YourRelatedModel.objects.filter(groups=request.user.groups.all()),
},
'field_name_that_is_fk': {
'queryset': YourOtherRelatedModel.objects.filter(slug=request_slug),
},
}
modelform = YourModelForm(jpic_field_options=lazy_field_options)
# after the modelform has called for parent __init__, it will set
# options for each field if possible.
</code></pre>
Freely redistributableModelForm ExtJS JSON Encoder
2008-12-27T23:24:17.532325-06:00tianhttp://djangosnippets.org/snippets/1264/<p>from http://www.djangosnippets.org/snippets/792/</p>
<pre><code> from utils.extjs import ExtJSONEncoder
from django.utils.safestring import mark_safe
class TestForm(forms.ModelForm):
class Meta:
model = TestModel
def as_ext(self):
return mark_safe(simplejson.dumps(self,cls=ExtJSONEncoder))
</code></pre>
Freely redistributableFieldAccessForm (per-field user access for forms derived from models)
2008-10-16T20:53:26.268032-05:00Killarnyhttp://djangosnippets.org/snippets/1148/<p>=== version 2 ===</p>
<blockquote>
<p>Parts of this code are based off of source from <em>davidcramer</em> on #django and I'd like to thank him for his assistance.</p>
</blockquote>
<p>Example:</p>
<pre><code># forms.py
...
class ForumPostForm(FieldAccessForm):
class Meta:
model = ForumPost
class FieldAccess:
moderator = FieldAccessLevel(
lambda user, instance: user.get_profile().is_moderator,
enable = ('approve', 'delete', …</code></pre>
Freely redistributablechange a widget attribute in ModelForm without define the field
2008-07-25T07:01:21.451388-05:00jediehttp://djangosnippets.org/snippets/916/<p>I will change a model form widget attribute without define the complete field. Because many "meta" information are defined in the model (e.g. the help_text) and i don't want to repeat this.</p>
<p>I found a solution: Add/change the widget attribute in the <strong>init</strong>, see example code.</p>
Freely redistributableReorder fields directly in the ModelForm
2008-05-16T06:44:32.617956-05:00HMhttp://djangosnippets.org/snippets/759/<p>Sometimes the order of the fields you get from a model needs to be adjusted when displaying its modelform. If it's just a few fields you can do it in the template, but what if you want to iterate over the form?</p>
<p>The fields are stored in a SortedDict, so …</p>
Freely redistributable