ModelForms

18 views
Skip to first unread message

giuliano....@gmail.com

unread,
Dec 11, 2013, 6:22:55 AM12/11/13
to django...@googlegroups.com

Hello,

when using ModelForms, which is the correct way to implement the "edit item" pattern?

Suppose I've a table with many rows, each representing a record in a database.
I choose one and wish to display/let the user edit the details.

I'm trying to use a ModelForm and fill some fields with specific data coming froma query.
The problem is I'm unable to initialize the fields and have the page served.

The arguments taken for a form do not work in a modelform.


class Quotation(models.Model):
    #...
    filename      = models.CharField("Filename", max_length=60)



    #...

class QuotationForm(ModelForm):
    class Meta:
        model = Quotation

    #...

def manage_quotations(request, quote_id):
    # retrieve quotation here
    # ...

    # just try to fill a form and have the page served
    qd = { 'filename' : '123' }

    form = QuotationForm()   # cannot pass qd here as initializer
    if request.method == "POST":
        pass   # manage posted data

    return render_to_response("quotes/manage_quotation.html", {
        "form": form,
    })



Tom Evans

unread,
Dec 11, 2013, 6:46:21 AM12/11/13
to django...@googlegroups.com
On Wed, Dec 11, 2013 at 11:22 AM, <giuliano....@gmail.com> wrote:
>
> Hello,
>
> when using ModelForms, which is the correct way to implement the "edit item"
> pattern?
>
> Suppose I've a table with many rows, each representing a record in a
> database.
> I choose one and wish to display/let the user edit the details.
>
> I'm trying to use a ModelForm and fill some fields with specific data coming
> froma query.
> The problem is I'm unable to initialize the fields and have the page served.
>
> The arguments taken for a form do not work in a modelform.

A ModelForm operates on an instance of a model. Eg

instance = Quotation(filename='123')
form = QuotationForm(instance=instance)

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages