mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	[3.0.x] Fixed #30931 -- Restored ability to override Model.get_FIELD_display().
Thanks Sergey Fedoseev for the implementation idea. Regression ina68ea23101. Backport of2d38eb0ab9from master
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							0644b73e85
						
					
				
				
					commit
					dd2ca8b0ea
				
			| @@ -767,8 +767,12 @@ class Field(RegisterLookupMixin): | ||||
|             if not getattr(cls, self.attname, None): | ||||
|                 setattr(cls, self.attname, self.descriptor_class(self)) | ||||
|         if self.choices is not None: | ||||
|             setattr(cls, 'get_%s_display' % self.name, | ||||
|                     partialmethod(cls._get_FIELD_display, field=self)) | ||||
|             if not hasattr(cls, 'get_%s_display' % self.name): | ||||
|                 setattr( | ||||
|                     cls, | ||||
|                     'get_%s_display' % self.name, | ||||
|                     partialmethod(cls._get_FIELD_display, field=self), | ||||
|                 ) | ||||
|  | ||||
|     def get_filter_kwargs_for_object(self, obj): | ||||
|         """ | ||||
|   | ||||
| @@ -21,3 +21,6 @@ Bugfixes | ||||
| * Fixed migrations crash on PostgreSQL when adding an | ||||
|   :class:`~django.db.models.Index` with fields ordering and | ||||
|   :attr:`~.Index.opclasses` (:ticket:`30903`). | ||||
|  | ||||
| * Restored the ability to override | ||||
|   :meth:`~django.db.models.Model.get_FOO_display` (:ticket:`30931`). | ||||
|   | ||||
| @@ -168,6 +168,16 @@ class GetFieldDisplayTests(SimpleTestCase): | ||||
|         self.assertIsInstance(val, str) | ||||
|         self.assertEqual(val, 'translated') | ||||
|  | ||||
|     def test_overriding_FIELD_display(self): | ||||
|         class FooBar(models.Model): | ||||
|             foo_bar = models.IntegerField(choices=[(1, 'foo'), (2, 'bar')]) | ||||
|  | ||||
|             def get_foo_bar_display(self): | ||||
|                 return 'something' | ||||
|  | ||||
|         f = FooBar(foo_bar=1) | ||||
|         self.assertEqual(f.get_foo_bar_display(), 'something') | ||||
|  | ||||
|     def test_iterator_choices(self): | ||||
|         """ | ||||
|         get_choices() works with Iterators. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user