#3148: Add getters and setters to model fields
---------------------------------------------------+------------------------
          Reporter:  [email protected]                 |         Owner:  Gulopine
            Status:  new                           |     Milestone:          
         Component:  Database layer (models, ORM)  |       Version:  SVN     
        Resolution:                                |      Keywords:          
             Stage:  Accepted                      |     Has_patch:  1       
        Needs_docs:  1                             |   Needs_tests:  0       
Needs_better_patch:  1                             |  
---------------------------------------------------+------------------------
Comment (by jerf):

 Using the model:

 {{{
 class GetSet(models.Model):
     field = models.CharField(max_length=200)

     @field.setter
     def field_setter(self, value):
         self._field = value + "_test_transform"
     @field.getter
     def field_getter(self):
         return self._field
 }}}

 we obtain with the latest patch "django-3148-approach-via-
 descriptors.diff" the following python REPL output:

 {{{
 Python 2.6.4 (r264:75706, Nov  8 2009, 19:34:30)
 [GCC 4.3.3] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> from mysite.modeltest.models import GetSet
 >>> example = GetSet(field = "test")
 >>> example.field
 'test_test_transform'
 >>> example.save()
 >>> example.id
 1
 >>> del example
 >>> example = GetSet.objects.get(id = 1)
 >>> example.field
 u'test_test_transform_test_transform'
 >>> example.save()
 >>> del example
 >>> example = GetSet.objects.get(id = 1)
 >>> example.field
 u'test_test_transform_test_transform_test_transform'
 >>>
 }}}

 That is not shippable behavior. The setter must not apply during
 construction from a database load. Loading and immediately saving an
 object with no changes must be a no-op, in terms of the stored data.
 Anything else is asking for disaster.

 I find myself wondering if perhaps the traffic on this bug is telling us
 that the interaction of properties when the values are being backed by a
 database store is just too complicated a concept to be worthwhile. When
 the setters and getters would apply is more subtle than it first appears,
 and perhaps "simple enough that it works as it first appears" (i.e., "no
 property behavior") is a better idea.

 I am not being sarcastic or passive aggressive. I am seriously wondering
 this. I now think the documentation would have to explain this in more
 detail than I originally thought, which adds a chunk of complexity right
 where Django's documentation needs it least, and I'm no longer convinced
 that's worth it. (Plus, after reviewing the latest SVN code, I still do
 not see a way to do this correctly without setting the flag during
 construction like I did in my original patch, which was not an...
 ''appreciated'' addition to the model loading code.)

-- 
Ticket URL: <http://code.djangoproject.com/ticket/3148#comment:34>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--

You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.


Reply via email to