How about:
def filter_keys(hash, keys):
# I'm convinced there must be a better way to write this
return dict([ item for item in hash.iteritems() if item[0] in
keys ])
def save(self):
Blog.objects.create(**filter_keys(self.clean_data, ['name',
'description'])
Although I can often omit the filter_keys, it's a fairly common idiom
in my code so I have it stashed globally anyways ;)
The other one is messier...
def set_initial_values_from_attrs(fields, from_object, keys):
for key in keys:
fields[key].initial = getattr(from_object, key)
def __init__(self, blog, *args, **kw):
super(MyForm, self).__init__(*args, **kw)
set_initial_values_from_attrs(self.fields, blog, ['title'])
... less typing, but also less clear...
- Paul
def __init__(self, blog, *args, **kwargs):
On Mar 7, 1:47 pm, "Amit Upadhyay" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have an enhancement request for new forms, instead of doing
>
> def save(self):
>
> > Blog.objects.create(title=self.clean_data["title"], description=
> > self.clean_data["description"])
>
> and
>
> def __init__(self, blog, *args, **kw):
>
> > super(MyForm, self).__init__(*args, **kw)
> > self.fields["title"].initial = blog.title
>
> It would be better to use:
>
> def save(self):
>
> > Blog.objects.create(title=self.clean_data.title, description=
> > self.clean_data.description)
>
> and
>
> def __init__(self, blog, *args, **kw):
>
> > super(MyForm, self).__init__(*args, **kw)
> > self.fields.title.initial = blog.title
>
> Its easier on eyes as well as fingers.
>
> Please! I love you, I beg you! :-)
>
> --
> Amit Upadhyay
> +91-9820-295-512
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" 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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---