mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	[1.11.x] Fixed #27434 -- Doc'd how to raise a model validation error for a field not in a model form.
Backport of e8c056c31a from master
			
			
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							31d7fc8541
						
					
				
				
					commit
					a1c6c220e2
				
			| @@ -322,6 +322,35 @@ pass a dictionary mapping field names to errors:: | ||||
|  | ||||
| Finally, ``full_clean()`` will check any unique constraints on your model. | ||||
|  | ||||
| .. admonition:: How to raise field-specific validation errors if those fields don't appear in a ``ModelForm`` | ||||
|  | ||||
|     You can't raise validation errors in ``Model.clean()`` for fields that | ||||
|     don't appear in a model form (a form may limit its fields using | ||||
|     ``Meta.fields`` or ``Meta.exclude``). Doing so will raise a ``ValueError`` | ||||
|     because the validation error won't be able to be associated with the | ||||
|     excluded field. | ||||
|  | ||||
|     To work around this dilemma, instead override :meth:`Model.clean_fields() | ||||
|     <django.db.models.Model.clean_fields>` as it receives the list of fields | ||||
|     that are excluded from validation. For example:: | ||||
|  | ||||
|         class Article(models.Model): | ||||
|             ... | ||||
|             def clean_fields(self, exclude=None): | ||||
|                 super(Article, self).clean_fields(exclude=exclude) | ||||
|                 if self.status == 'draft' and self.pub_date is not None: | ||||
|                     if exclude and 'status' in exclude: | ||||
|                         raise ValidationError( | ||||
|                             _('Draft entries may not have a publication date.') | ||||
|                         ) | ||||
|                     else: | ||||
|                         raise ValidationError({ | ||||
|                             'status': _( | ||||
|                                 'Set status to draft if there is not a ' | ||||
|                                 'publication date.' | ||||
|                              ), | ||||
|                         }) | ||||
|  | ||||
| .. method:: Model.validate_unique(exclude=None) | ||||
|  | ||||
| This method is similar to :meth:`~Model.clean_fields`, but validates all | ||||
|   | ||||
		Reference in New Issue
	
	Block a user