Login

Tag "modelforms"

Snippet List

Readonly fields on Form/Modelform

A Form and ModelForm which provides the ability to specify certain fields as readonly, meaning that they will display their value as text wrapped with a <span> tag. The user is unable to edit them, and they are protected from POST data insertion attacks. The recommended usage is to place a NewMeta inner class on the form, with a readonly attribute which is a list or tuple of fields, similar to the fields and exclude attributes on the Meta inner class. class MyForm(ReadonlyForm): foo = forms.TextField() bar = forms.TextField() class NewMeta: readonly = ('foo',) Use these forms as you would a standard form in your templates.

  • forms
  • field
  • readonly
  • modelforms
  • span
Read More

ModelForm-based create_update generic views

This is a ModelForms-based rewrite of the create_object and update_object generic views, with a few added features. The views now accept a "form_class" argument optionally in place of the "model" argument, so you can create and tweak your own ModelForm to pass in. They also accept a "pre_save" callback that can make any additional changes to the created or updated instance (based on request.user, for instance) before it is saved to the DB. Usage: just save the code in a file anywhere on the PythonPath and use the create_object and update_object functions as views in your urls.py.

  • newforms
  • views
  • generic
  • update
  • modelforms
  • create
Read More

Class ModelInfo for show object info

This class works in compatibility with a ModelForm, but instead of show a form, it shows the field values. The Meta class attributes can be used to customize fields to show, to be excluded, to divide by sections, to urlize text fields, etc. **Example 1:** class InfoSingleCertificate(ModelInfo): class Meta: model = SingleCertificate sections = ( (None, ('vessel','description','document','comments','status',)), ('Issue', ('issue_date','issue_authority','issue_location',)), ) **Example 2:** class InfoSingleCertificate(ModelInfo): class Meta: model = SingleCertificate fields = ('vessel','description','document','comments','status', 'issue_date','issue_authority','issue_location',) **How to use:** Just save this snippet as a file in your project, import to your views.py module (or a new module named "info.py") and create classes following seemed sytax you use for ModelForms.

  • forms
  • model
  • display
  • modelforms
  • info
  • values
Read More

Multiple inheritance of newforms and modelforms

If you try to use multiple inheritance with a modelform (to mix in some fields from an already existing form class for example) you'll get the following rather terrifying error: > "Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases" The solution is to first create the ModelForm, then create a NEW class that inherits from both the ModelForm and the form you want to mixin, then finally apply the recipe from here: [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197)

  • newforms
  • inheritance
  • modelforms
  • multipleinheritance
  • metaclasses
  • metaclass
Read More

4 snippets posted so far.