Please comment on this small newforms sample

0 views
Skip to first unread message

gordyt

unread,
Dec 23, 2006, 8:18:52 PM12/23/06
to Django users
Howdy Folks!

I'm very new to Django and have been experimenting with newforms. I
would be most grateful if anyone would care to take a look at this
small sample to see if I'm doing things in an efficient manner. It
works great, but I want to make sure I'm not doing something stupid..
:-) To shorten the following code snippets I've yanked out all
non-essential parts.

The following is for entering a new Customer or editing an existing
one.

Thanks very much for any insight or hints!

--gordon

************************************************************
>From models.py:
************************************************************

class CustomerType(models.Model):
description = models.CharField(maxlength=32)

class Customer(models.Model):
name = models.CharField(maxlength=200)
customer_type=models.ForeignKey(CustomerType)
notes = models.TextField(blank=True,default="")

************************************************************
>From forms.py:
************************************************************

class AddEditCustomerForm(forms.Form):
name = forms.CharField(max_length=200)
customer_type_id = forms.ChoiceField(label="Customer Type",
choices=[(c.id,c.description) for c in
CustomerType.objects.all()])
notes = forms.CharField(widget=forms.Textarea(attrs={'rows':
'3'}),required=False)


************************************************************
>From views.py:
************************************************************

def add_edit_customer(request,object_id):
"""
Edit an existing customer (object_id > 0) or add a new one
(object_id == 0)
"""
object_id = int(object_id)
error_message = None
if request.method == "POST":
edit_form = AddEditCustomerForm(request.POST)
if edit_form.is_valid():
customer = Customer(**edit_form.clean_data)
if object_id > 0 : customer.id = object_id
if Customer.objects.filter(

name=customer.name,customer_type=customer.customer_type).exclude(
id=object_id).count() > 0:
error_message = "Duplicate customer."
else:
customer.save()
return HttpResponseRedirect("/kindledb/customers/%i/" %
customer.id)
elif object_id > 0:
edit_form =
AddEditCustomerForm(Customer.objects.get(id=object_id).__dict__)
else:
edit_form = AddEditCustomerForm()
return render_to_response("kindledb/add_edit_customer.html",
{"edit_form": edit_form, 'object_id': object_id,
'error_message': error_message})

************************************************************
Here is an extract from the template (add_edit_customer.html):
************************************************************

{% block content %}
{% if error_message %}
<p>{{error_message}}</p>
{% endif %}
<form method="POST" action=".">
<input type="hidden" name="object_id" value="{{object_id}}" />
<table class="edit_form">
{{edit_form}}
<tr><th></th><td><input type="submit" value="Save Changes"
/></td></tr>
</table>
</form>
{% endblock %}

alex.v...@gmail.com

unread,
Dec 26, 2006, 12:54:13 AM12/26/06
to Django users

Hi !

> customer = Customer(**edit_form.clean_data)

Thanks! you gave me a good hint! :) I was unsure how to pass data, now
I understand that its
easy :) I will port my code to use ** to pass clean data now.

Btw, you can see my feedback on django.newforms here:
http://groups.google.com.ua/group/django-developers/msg/5e14ffb43e72644a

Alex

Adrian Holovaty

unread,
Dec 26, 2006, 6:07:10 PM12/26/06
to django...@googlegroups.com
On 12/23/06, gordyt <gor...@gmail.com> wrote:
> I'm very new to Django and have been experimenting with newforms. I
> would be most grateful if anyone would care to take a look at this
> small sample to see if I'm doing things in an efficient manner. It
> works great, but I want to make sure I'm not doing something stupid..
> :-) To shorten the following code snippets I've yanked out all
> non-essential parts.

Hi Gordon,

That looks pretty good. Once I finish up the form_for_model() helper
function, you might be able to cut down on the amount of code even
further, because the AddEditCustomerForm could be generated from the
model. (Of course, you might not want to do that, depending on whether
your model has fields that you don't want to edit via that form.)

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

gordyt

unread,
Dec 26, 2006, 6:56:57 PM12/26/06
to Django users
Thanks Adrian and Alex!

Adrian: gotta tell ya I really enjoy working with Django. We had a
nice talk about it at our Python user's group meeting here in Houston
on 12/19. One of the guys there is the author of the
http://houstoncrimemaps.com/ site (pure Django!)

--gordy

Reply all
Reply to author
Forward
0 new messages