Re: Generic views and manipulating data before saving

2006-08-27 Thread konryd

I guess manipultors are made for this purpose. I'm not sure but you can
probably override AddManipulator in order to use generic views.

Hope this is the answer to your queston.

This is an example from my excercise blog app (it doesn't use  generic
view actually):

#model
class Entry(models.Model):
headline = models.CharField(maxlength=100, default='Default headline')
pub_date = models.DateTimeField(default=datetime.now())
body = models.TextField()

class Admin:
pass

def __str__(self):
return self.headline


# Custom manipulator (I keep this code in views.py)
class NoDateAddManipulator(forms.Manipulator):
def __init__(self):
self.fields = (
forms.TextField(field_name="headline", 
is_required=True),
forms.LargeTextField(field_name="body", 
is_required=True),
)
def save(self,new_data):
### Here you can make your altering
#
e = Entry(headline = new_data["headline"], body = 
new_data["body"])
e.save()
return e

# And the view
# this is copied form django docs (even comments stayed:D)

def add(request):
manipulator = NoDateAddManipulator()
if request.POST:
new_data = request.POST.copy() # If data was POSTed, we're 
trying to
create a new Place.
errors = manipulator.get_validation_errors(new_data)
if not errors:
manipulator.do_html2python(new_data)
new_entry = manipulator.save(new_data)
return HttpResponseRedirect("/blog/%i/saved" % 
new_entry.id)
else:
errors = new_data = {} # No POST, so we want a brand new form 
without
any data or errors.
# Create the FormWrapper, template, context, response.
form = forms.FormWrapper(manipulator, new_data, errors)
return render_to_response('blog/add_form.html', {'form': form})


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Generic views and manipulating data before saving

2006-08-26 Thread Andres Luga

Hi,

I've spent many hours trying to get the following to work (I'm not
blaming Django - I'm very new to Python and Django, it was late etc).
To hopefully make the solution easier to find in Google, I write it
here. Suggestions for improvement are welcome.

Question: using generic views, how to alter the data user has POST-ed?
For example for setting the "last updated by"-field.
models.py:  lastupdatedby = models.ForeignKey(User)
*_detail.html: {{object.lastupdatedby}}

In the views.py I have a wrapper method, that modifies the data that
has been POST-ed and then passes it along to generic views for
validating and saving.

@login_required
def limited_update_object(*args, **kwargs):
  #The fields that are handled by Django are in "follow", for example:
  #follow = {'lastupdatedon': False}   <-- auto_now=True
  #kwargs['follow'] = follow

  request = args[0]<-- is this safe?
  if request.POST:
new_data = request.POST.copy()
new_data[ 'lastupdatedby' ] = str( request.user.id )
request._post = new_data

  return update_object(*args, **kwargs)


The question I have yet to find an answer: is there a more convenient
way for debugging and examining the data than raise Http404,
str(new_data)?

Regards,
Andres

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---