Do not check ManyToMany field in a model field

Such checks must be done in a ModelForm clean() method as ManyToMany
field cannot be manipulated on an as-yet-to-be-saved object.
This commit is contained in:
Benjamin Dauvergne 2011-10-18 12:28:12 +02:00
parent 3a1d8fea08
commit deb521506b
4 changed files with 16 additions and 6 deletions

View File

@ -111,6 +111,7 @@ class ContentAdmin(admin.ModelAdmin):
class AutomaticForwardingAdmin(admin.ModelAdmin):
filter_horizontal = [ 'filetypes', 'originaly_to_user', 'forward_to_user',
'forward_to_list' ]
form = forms.AutomaticForwardingForm

View File

@ -7,8 +7,10 @@ from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from django.contrib.admin.widgets import FilteredSelectMultiple as AdminFilteredSelectMultiple
from django.forms import ValidationError
from models import Document, username, MailingList, FileType, Content, AttachedFile
from models import Document, username, MailingList, FileType, Content, \
AttachedFile, AutomaticForwarding
from uni_form.helpers import FormHelper, Submit, Layout, ButtonHolder
import pyuca
from widgets import TextInpuWithPredefinedValues, JqueryFileUploadInput
@ -179,3 +181,14 @@ class DelegationForm(Form):
first_name = CharField(label=_('Firstname'))
last_name = CharField(label=_('Lastname'))
email = EmailField(label=_('Email'))
class AutomaticForwardingForm(ModelForm):
class Meta:
model = AutomaticForwarding
def clean(self):
cleaned_data = super(AutomaticForwardingForm, self).clean()
if not cleaned_data.get('forward_to_user') and not cleaned_data.get('forward_to_list'):
raise ValidationError(_('A forwarding rule must have at least one recipient, person or list.'))
return cleaned_data

View File

@ -194,10 +194,6 @@ class AutomaticForwarding(Model):
+ list(self.forward_to_list.all())) }
return _('Forward documents of type %(filetypes)s automatically to %(to)s') % ctx
def clean(self):
if not self.forward_to_user.count() and not self.forward_to_list.count():
raise ValidationError(_('A forwarding rule must have at least one recipient, person or list.'))
class Meta:
verbose_name = _('Automatic forwarding rule')
verbose_name_plural = _('Automatic forwarding rules')

View File

@ -222,7 +222,7 @@ msgstr "Transfert les documents du type %(filetypes)s automatiquement à %(to)s"
#: docbow/models.py:197
msgid "A forwarding rule must have at least one recipient, person or list."
msgstr ""
"Un destinataire d'origine, au moins, doit correspondre pour que la règle s'applique."
"Une règle de transfert doit contenir au moins un nouveau destinataire; une personne ou une liste."
#: docbow/models.py:200
msgid "Automatic forwarding rule"