django-xfield

A Django utility package to handle zero or more form inputs of the same name
Download

django-xfield Ranking & Summary

Advertisement

  • Rating:
  • License:
  • GPL v3
  • Publisher Name:
  • Chang Phui Hock
  • Publisher web site:
  • https://github.com/phuihock/

django-xfield Tags


django-xfield Description

A Django utility package to handle zero or more form inputs of the same name django-xfield is a Django utility package to handle zero or more form inputs of the same name.It is not uncommon to find a situation where you need to allow zero or more values for the same field. For example:Enter your favorite colors:1. 2. 3. As an alternative to Django's Formset, you can choose to do the following with django-xfield:# forms.pyclass FavColorForm(forms.Form): colors = ExpandableField(fields.CharField, min_values=3, max_length=100)# views.pydef echo_fav_colors(request): form = FavColorForm(request.POST) if form.is_valid(): # because 'colors' is an expandable field, it returns a list of all values submitted by the name 'colors' colors = self.cleaned_data assert colors == ...django-xfield introduces 2 classes (they are closures in disguise): ExpandableField and ExpandableWidget, both are factories that return a subclass of the class that you provide as the first positional argument. ExpandableField accepts 2 optional arguments - min_values and max_values and pass the rest of the arguments, if provided, to the actual field class for initialization.min_values the minimum number of values that user must provided, default to 0max_values the maximum number of values that user can supply, default to None (WARNING not implemented)For example, "colors" is assigned a subclass of CharField that returns a list with a minimum of 3 items/values in it.Just like a normal field, you can supply a different widget if the default is not what you want. However, the widget must be an instance of the class that ExpandableWidget returns (recall that ExpandableWidget is a closure, not a class). For example:class FavColorForm(forms.Form): colors = ExpandableField(fields.CharField, min_values=3, max_length=100, widget=ExpandableWidget(Textarea, min_values=3))NOTE It is necessary to pass the same min_values/max_values if you are supplying a custom ExpandableWidget.This simplify the server-side as well as client-side (Javascript) programming. For a list of working examples, please visit http://demo.phuihock.com/xfield/. Alternatively, you can also run the demo locally:git clone git://github.com/phuihock/django-xfield.gitvirtualenv --distribute django-xfieldcd django-xfieldsource bin/activatepip install -r req.txtpython manage.py runserverThen, go to http://127.0.0.1:8000/What works for me may not work for everyone else. If you find this utility useful and need example of specific use case, I'll be glad to provide some. Also, checkout the demo app.INSTALLATIONpip install django-xfieldThis is not a Django app, so you don't have to add it to INSTALLED_APPS. Requirements: · Python · Django


django-xfield Related Software