On Tue, Sep 27, 2011 at 7:10 AM, Jack720 <fuzzyfo...@yahoo.com> wrote:
> I'd just like to state that I did try and find this out on my own
> first but "django form model api" isn't a vary narrowing search. I've
> read through the documentation multiple times but I'm still a little
> stuck. It would appear most of the documentation/examples are
> outdated. I'm certain this has been answered before I just can't find
> the answer.
>
> First, is the current forms api solid? Or are major changes still
> planed? It seems like it went through a major change in 2007 and now
> 2010? The newforms versus forms gets a bit hairy.

I'm not sure what you're referring to with the 2010 change. The forms
API has been very much stable since 2007, and is covered by Django's
backwards compatibility policy.

> 2ndly, As a bit of constructive criticism the main tutorial should
> really have included a section on the forms api. At the very least the
> end of the tutorial should let you know it exists. I know how time
> keeps documentation from getting done. I get the whole learning to
> multiply before using a calculator thing but I didn't even know about
> that part of the API for a while and it's fairly major. When it comes
> to forms the tutorial leaves us at multiplication and didn't even tell
> us there was a calculator.

Thanks for the feedback -- we're certainly aware that more tutorials
are needed, we just haven't found the time (or the willing volunteers)
to write them. Forms would certainly be a good candidate for a new
tutorial.

> 3rd, and the main question. Does anyone have a simple working example!
> I'm trying to use the form api using my models but the code in the
> documentation seems to have a bit of a chicken egg problem. They seem
> to show a view that displays a new form or posts and saves a form but
> doesn't show how we got the form for a specific record id in the first
> place! I'm just trying to create a simple list of records with CRUD.
> simple enough just like the admin interface only I'll be putting a
> bunch of other code around this.

Well, the pieces are all there [1] [2]; it's just not obvious how to
put them all together. Here's an example of an update view:

from django import forms
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render
from myapp.models import MyModel

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

def my_view(request, object_id):
    try:
        obj = MyModel.objects.get(pk=object_id)
    except MyModel.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = MyForm(instance=obj, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/some/url/')
    else:
        form = MyForm(instance=obj)

    return render(request, 'example/template.html' {
        'form': form
    })

The only differences between this example and the simple form example are:
  * You start the view by retrieve an object matching the view
arguments, raising 404 if the object can't be found
  * You pass that instance to the form when you construct the form instance.

I know this isn't the same as a full length tutorial, but I hope it's
enough to get you going.

[1] https://docs.djangoproject.com/en/1.3/topics/forms/
[2] 
https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#the-save-method

Yours,
Russ Magee %-)

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to