mirror of
				https://github.com/django/django.git
				synced 2025-10-30 17:16:10 +00:00 
			
		
		
		
	[1.7.x] Fixed #23473 -- Documented that @deconstructible classes need __eq__.
Backport of 066e672d79 from master
			
			
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							40e5eb4114
						
					
				
				
					commit
					cf78a0ccc3
				
			| @@ -594,9 +594,14 @@ of three things ``(path, args, kwargs)``: | |||||||
| Django will write out the value as an instantiation of your class with the | Django will write out the value as an instantiation of your class with the | ||||||
| given arguments, similar to the way it writes out references to Django fields. | given arguments, similar to the way it writes out references to Django fields. | ||||||
|  |  | ||||||
|  | To prevent a new migration from being created each time | ||||||
|  | :djadmin:`makemigrations` is run, you should also add a ``__eq__()`` method to | ||||||
|  | the decorated class. This function will be called by Django's migration | ||||||
|  | framework to detect changes between states. | ||||||
|  |  | ||||||
| As long as all of the arguments to your class' constructor are themselves | As long as all of the arguments to your class' constructor are themselves | ||||||
| serializable, you can just use the ``@deconstructible`` class decorator | serializable, you can use the ``@deconstructible`` class decorator from | ||||||
| from ``django.utils.deconstruct`` to add the method:: | ``django.utils.deconstruct`` to add the ``deconstruct()`` method:: | ||||||
|  |  | ||||||
|     from django.utils.deconstruct import deconstructible |     from django.utils.deconstruct import deconstructible | ||||||
|  |  | ||||||
| @@ -604,8 +609,13 @@ from ``django.utils.deconstruct`` to add the method:: | |||||||
|     class MyCustomClass(object): |     class MyCustomClass(object): | ||||||
|  |  | ||||||
|         def __init__(self, foo=1): |         def __init__(self, foo=1): | ||||||
|  |             self.foo = foo | ||||||
|             ... |             ... | ||||||
|  |  | ||||||
|  |         def __eq__(self, other): | ||||||
|  |             return self.foo == other.foo | ||||||
|  |  | ||||||
|  |  | ||||||
| The decorator adds logic to capture and preserve the arguments on their | The decorator adds logic to capture and preserve the arguments on their | ||||||
| way into your constructor, and then returns those arguments exactly when | way into your constructor, and then returns those arguments exactly when | ||||||
| deconstruct() is called. | deconstruct() is called. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user