Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » genric views or formfields/manipulators? [RSS Feed]

#1 Nov. 24, 2005 15:18:03

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

genric views or formfields/manipulators?


I need a simple form to let the user enter some parameters to generate
a report from data present in the database. Which is the correct way to
proceed?
I created a model "Report_Request":

class Report_Request(meta.Model):
city = meta.ForeignKey(City)
date_start = meta.DateField("From")
data_stop = meta.DateField("To")
alldetails = meta.BooleanField("All Details", default=True)

I don't really need to create (or better, save()) an instance of this
model to produce the template: I just need to get those parameters from
a POST of the HTML form. Anyway having the model is good to use
manipulators and formfields to handle validation etc. Is there a
"generic view" solution without using manipulators/formfields directly
? Which generic view should be better for this purpose?

Thanks in advance,

Emanueke

: then retrieve the proper data from db and render a template with
those data, but it doesn't matter so much here.

Offline

#2 Nov. 25, 2005 00:07:07

s.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

genric views or formfields/manipulators?


I had much the same problem and solved it using views and forms in the
following way:

create a template were, for example, the first part is a form with
selection items like start date, end date (providing a date range),
etc. Then, when the user POST's this form via a submit button, capture
the selections in a view and hand the selections to a get_...()
function defined on the target object (_module_get...()), that provides
a list of items to display in the current form, just below the
selection items. The template used is using a <form> statement for the
selection items, and a trivial part for displaying the data. If that
doesn't make sense, I could send you the source code I use.

Offline

#3 Nov. 25, 2005 14:29:29

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

genric views or formfields/manipulators?


First of all thanks for the answer. As far as I understand you set up a
two step form in the same template. This could solve some of my
problems, but not exactly the one I posted; in any case please post the
code (a simple example would be very appreciated), because examples are
exactly what djangonauts likes :)

Coming back to my original problem, it's just this question: is there a
generic view just for getting and validating data from a form built on
a given model? I mean without saving the instance of the model to the
db (without ".save()") but just redirecting the validated POST to a
proper view (note that this is not a "create" action).
Or is it better to do it by hand using manipulators and formfields?

Cheers,

Emanuele

Offline

#4 Nov. 25, 2005 23:32:53

s.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

genric views or formfields/manipulators?


Yes, of course, should have read the question properly. Anyhow, I
haven't found a readymade generic view to do the job for me so here's
the sample I use:

-------------- view: ----------------------------
@login_required
def projects(request):
"""Get list of projects and project details for logged in manager."""

users =
project_list =
start_date = ''
end_date = ''
summary = False

for p in ttime.projects.get_managed_by(users):
project_list.append((p.id, p.project))

if request.POST:
for key, value in request.POST.items():
if key == 'project' : project = int(value)
if key == 'start_date': start_date = value
if key == 'end_date' : end_date = value
if key == 'summary' : summary = bool(value)

proj_list = ttime.projects.get_project_items(
users, project, start_date, end_date)

return render_to_response('ttime/projects',
{'title': 'Project report', 'project': project,
'start_date': start_date, 'end_date': end_date})

----------- template 'ttime/projects':----------------
{% extends "base_site" %}
{% block content %}<div id="content-main" class="module">
<form action="" method=post>
<fieldset class="module aligned">
<h2>Selections</h2>
<table width=100%>
<thead>
<tr>
<th class=optional>Project</th>
<th class=optional>Start date</th>
<th class=optional>End date</th>
<th style=tex-align:centre>
<input type=submit name=summary value=Summary />
</th>
</tr>
</thead>
<tr class=row1>
<td>
<select id=id_project class=vSelectField name=project size=1>
{% for proj in project_list %}
<option {% ifequal proj.0 project %}selected{% endifequal %}
value={{proj.0}}>{{proj.1}}</option>
{% endfor %}
</select>
</td>
<td>
<input type=text id=id_start_date class=vDateField name=start_date
size=10 value="{{start_date}}" maxlength=10 />
</td>
<td>
<input type=text id=id_end_date class=vDateField name=end_date
size=10 value="{{end_date}}" maxlength=10 />
</td>
</tr></table></fieldset></form>

<h2>Projects - detailed</h2>
<table width=100%>
<thead>
<th>Project / Subproject / Activity</th>
</thead>
{% for p in proj_list %}
<tr>
<td><h3>{{p.project}} - {{p.name}}</h3></td>
</tr>
</table>
------------------------------------------
The end result is that the user(s) get presented with some selection
items, including a project popup selection box, hit the summary button,
and the resulting batch of records get displayed below the selection
box, maintaining the selections made.

Hope this helps.
/LarS

Offline

#5 Nov. 25, 2005 23:50:33

oggie r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

genric views or formfields/manipulators?


Hi Emanuele,
I think the answer is no, but there is a quick and easy solution. Check
out the django.views.generic.create_object.py file and the
create_object function. You can see that the save() occurs via
manipulator.save(new_data).
>From what I have learned, it looks like you have two options:
1) Create your own "generic" method that does not actually save, this
would be a good start. This would also have to be coordinated with the
urls.py file so that it is used appropriately (e.g. to override any
admin behaviour). This can be done without modifying django libraries.
2) modify manipulator_save() or the man.save() call in
get_manipulator() in django.core.meta.__init__.py. This would involve
source changes of course but may be easier depending on how much you
want to override.

At least, that is what I would consider. But I would recommend trying
alternatives before writing your own custom views - you may save
yourself a lot of time if a lot of views require customization.

-rob

Offline

  • Root
  • » Django
  • » genric views or formfields/manipulators? [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of January
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support