Login

Tag "unique"

Snippet List

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

True Unique Boolean Model Decorator

This class decorator will help you when you want to keep a unique boolean (think a 'default' field which should only be only one set to true in a group). The interesting thing with this, is that it's possible to assign a subset of fields to be used, so that the uniqueness of the field will be guaranteed among only the subset (look at the Example section in the code to understand this behaviour). This is a better and improved way of doing http://djangosnippets.org/snippets/2676/

  • model
  • unique
  • boolean
Read More

True Unique Boolean Decorator

Useful when you want to keep only one instance of a model to be the default one. This is a decorative way of doing the same as in http://djangosnippets.org/snippets/1830/ Can this be made better as a class decorator (not having to declare explicitly the save method) ?

  • model
  • unique
  • boolean
Read More

Model Forms: Clean unique field

Often I want fields in my models to be unique - Django's `unique` works too late in model form validation and will throw an exception unless you check for it by hand. This is a bit of code that cleans up the boiler plate of checking if different fields are unique. Set `exclude_initial` to `False` if you want to raise an error if the unique field cannot be set to whatever value the instance had before. **Update** Thanks fnl for rightly pointing out that Django's `unique=True` does check for this - just make sure to pass `instance=obj` when you're initializing your forms. _HOWEVER_ a problem you'll typically run into with this is though you want a field to unique, you also want multiple entries to be able to be `blank`. This can help you!

  • forms
  • model
  • unique
  • model-forms
Read More

isUnique validator for newforms

This is a generic unique field value validator for use with newforms. ( It's handy to plug into newforms-admin.) Example, with newforms-admin: ` class LinkAdminForm( ModelForm ): def clean_url( self ): return isUnique( self.instance, 'url', self.cleaned_data['url']) class LinkAdmin( ModelAdmin ): form = LinkAdminForm site.register( Link, LinkAdmin ) `

  • newforms
  • admin
  • unique
Read More

Validation for 'unique' and 'unique_together' constraints (different version)

This is a slightly different and extendend version of this snippet: http://www.djangosnippets.org/snippets/260/ Unique constraints for single fields are validated in a clean_FIELD, instead of globally in the form's clean() method, so that the error messages are correctly assigned to each field. Additionally, you can specify mappings for unique_together constraints to assign those error messages to a specific field as well (instead of having them in non_field_errors(), where they would normally be.

  • newforms
  • forms
  • validation
  • unique_together
  • unique
  • constraints
Read More

Add validation for 'unique' and 'unique_together' constraints to newforms created dynamically via form_for_model or form_for_instance

This snippet provides a form_for_model and form_for_instance function which create newforms that respect the unique and unique_together constraints defined in the model. One thing about the coding style in this code snippet: Classes are capitalized even if they're passed as arguments. Thus "Model" is a model class while "model" is a model instance.

  • newforms
  • form_for_model
  • form_for_instance
  • unique_together
  • unique
  • formfield_factory
Read More

7 snippets posted so far.