#3148: Add getters and setters to model fields
-----------------------------+----------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: telenieko
Status: assigned | Component: Database wrapper
Version: SVN | Resolution:
Keywords: | Stage: Accepted
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
-----------------------------+----------------------------------------------
Changes (by telenieko):
* keywords: MESSAGE =>
Comment:
Replying to [comment:10 SmileyChris]:
Before anything, if you look at the attached patch from TRAC it might seem
that everything is on the same function (`Field.__init__()`,
but that's not the case, there are two functions patched here:
`Field.__init__()` and `Field.contribute_to_class()`. (See the line marked
with dots in TRAC's diff viewer).
> 1. Using the reserved word "`property`" sounds like it'll get you in to
trouble. In fact, I can't see how the patch would even work since you try
to `setattr` to `property(getter, setter, delter, docter))` but `property`
would be defined as the tuple! How about using `property_` or
`use_property` or something.
Well, there are tests included with the patch and they don't fail! ;)
Anyway; It does not crash because the callable property() is not used in
`Field.__init__()` for anything. So it's safe to use it there.
Then we save the parameter in `Field.property` which can never be accesed
as `property` outside of `Field.__init__()`, it will always be
`self.property`.
The reason I used property= instead of something else was to make it clear
what the parameter is about, and give an idea of how it works (just
exactly as property())
> 2. why do you need to keep the `property` tuple around? (i.e.
`self.property = property`)
Because property is read in `Field.__init__()` and used then in
`Field.contribute_to_class()` to create the property in the model. So I
need to save the property parameter from __init__() to use it in
contribute_to_class(). Also I wanted to be consistent, the first Thing
`Field.__init__()` does is save all the kwargs on `self`. Anyway, it's
needed for contribute_to_class.
> 3. instead of your __add__ magic, how about something simpler, like:
> {{{
> #!python
> if len(use_property) < 2
> # getter and setter are both required.
> raise ValueError("You must specify at least a getter and a setter
method with use_property")
> # Create a property on ``cls`` with the methods given.
> setattr(cls, self.name, property(*use_property))
> }}}
Uhm.. really nice idea, I'll update the patch later with this change.
Before updating, do you really thing the parameter to Field should not be
`property`? As explained aboved, it will never cause a namespace collision
until somebody wants to use property() in `Field.__init__()` is is
unlikely to happen due to what `Field.__init__()` does.
--
Ticket URL: <http://code.djangoproject.com/ticket/3148#comment:11>
Django Code <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
-~----------~----~----~----~------~----~------~--~---