How to get this auto-field id value

257 views
Skip to first unread message

David

unread,
Aug 11, 2009, 12:20:08 PM8/11/09
to Django users
Hello,

I am trying to retrieve an autoField id value in my following code
when I update records.

def update_record(request):
if request.POST:
form = Model_Form(request.POST)
if form.is_valid():
form.save(pk=form.id, force_update=True)

I got an error that "Model_Form" object has no attribute 'id'.

Model_Form is a model-based form class that has an autoField id that
increases one by one. When I update a record, I need to give this
value.

I even tried "pk=form.pk", however it did not work either.

Any suggestions how to fix this?

Thanks so much.

David

unread,
Aug 11, 2009, 12:26:27 PM8/11/09
to Django users
Just a little more information. The autoField id is in the model
class. Model_Form is a form based on this model class.

David

unread,
Aug 11, 2009, 8:27:06 PM8/11/09
to Django users
Any tips/suggestions are highly appreciated...
> > Thanks so much.- Hide quoted text -
>
> - Show quoted text -

Malcolm Tredinnick

unread,
Aug 11, 2009, 9:51:01 PM8/11/09
to django...@googlegroups.com
On Tue, 2009-08-11 at 17:27 -0700, David wrote:
> Any tips/suggestions are highly appreciated...

One tip is to wait more than eight hours before posting "please help me"
yet again.

>
>
>
> On Aug 11, 9:26 am, David <Ww...@yahoo.com> wrote:
> > Just a little more information. The autoField id is in the model
> > class. Model_Form is a form based on this model class.
> >
> > On Aug 11, 9:20 am, David <Ww...@yahoo.com> wrote:
> >
> >
> >
> > > Hello,
> >
> > > I am trying to retrieve an autoField id value in my following code
> > > when I update records.
> >
> > > def update_record(request):
> > > if request.POST:
> > > form = Model_Form(request.POST)
> > > if form.is_valid():
> > > form.save(pk=form.id, force_update=True)
> >
> > > I got an error that "Model_Form" object has no attribute 'id'.
> >
> > > Model_Form is a model-based form class that has an autoField id that
> > > increases one by one. When I update a record, I need to give this
> > > value.
> >
> > > I even tried "pk=form.pk", however it did not work either.

ModelForm subclasses don't include any AutoField values. By definition,
they are *automatically* assigned by the database. If you're using an
AutoField, you don't set it yourself. If you want to manage the primary
key value yourself, don't make it an AutoField.

Regards,
Malcolm


Reply all
Reply to author
Forward
0 new messages