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 %}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django 
users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to