straight55b / app-engine-patch

Automatically exported from code.google.com/p/app-engine-patch
0 stars 0 forks source link

Best use of django forms and template functions with expando classes #94

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
*************************************************************************
(Q1) First is there a group, list or IRC where one can ask questions about
using django with your app engine patch?
*************************************************************************

The main question I have is: 
*************************************************************************
(Q2) "How can I use the best use the form classes
      and template functions with expando attributes?"
*************************************************************************

I have:

1) subclassed BaseModelForm
  a) modifying __init__ to add the _dynamic_properties to object_data
       (source extract at bottom)
  b) modifying save to pull the values I know are there into converted_data
       (source extract at bottom)

2) Written a form (subclass of djangoforms.ModelForm) 
   with the standard and expando fields defined

3) Written templates to manually render the standard & expando values

But (1b) is a terrible kluge and (3) is more work than necessary

*************************************************************************
(Q2.1) Is there a better way to accomplish this which will be I could use
    the template functions e.g. form.as_p etc. and use generic code rather
    than the (1b) hack?
*************************************************************************

Extracts
========

__init__:
...
    if instance is not None:
      for name, prop in instance.properties().iteritems():
        if opts.fields and name not in opts.fields:
          continue
        if opts.exclude and name in opts.exclude:
          continue
        object_data[name] = prop.get_value_for_form(instance)
      ##@@ Mod to add dynamic properties
      dynamic_properties = getattr(instance, '_dynamic_properties', None)
      if dynamic_properties:
        for key in dynamic_properties.keys():
          object_data[key] = dynamic_properties[key]
      ##@@ end Mod
    if initial is not None:
...

save:
(value is expando data item)

...
    for name, prop in propiter:
      value = cleaned_data.get(name)
      if value is not None:
        converted_data[name] = prop.make_value_from_form(value)
    ##@@ mod
    dynamic_value = 'value' in self.data and self.data['value']
    if dynamic_value:
        converted_data['value'] = dynamic_value 
    ##@@ end mod
    try:
...

Original issue reported on code.google.com by Tim.Jone...@gmail.com on 27 Feb 2009 at 1:00

GoogleCodeExporter commented 9 years ago
I'd suggest that you state this question on the App Engine group:
http://groups.google.com/group/google-appengine/

I never needed to work with Expando and right now I don't have time to think 
about a
solution. ModelForm just isn't really designed for manipulating a dynamic list 
of fields.

Original comment by wkornew...@gmail.com on 27 Feb 2009 at 2:09