On 9/5/2010 3:12 AM, Jagdeep Singh Malhi wrote:
> I try to get the max value of id from database using max() function
> but a face this error.
> {
> TypeError at /add_db/
> 
> 'builtin_function_or_method' object is not iterable
> 
> Request Method:       POST
> Request URL:  http://localhost/django/add_db/
> Django Version:       1.2.1
> Exception Type:       TypeError
> Exception Value:      'builtin_function_or_method' object is not iterable
> 
> Exception Location:   /home/jagdeep/mysite/add_db/views.py in add_db,
> line 19
> Python Executable:    /usr/bin/python
> }
> 
> model.py
> from django.db import models
> from django.forms import ModelForm
> 
> class Input(models.Model):
>     input1 = models.FloatField()
>     input2 = models.FloatField()
> 
> class Output(models.Model):
>       out = models.ForeignKey(Input)
>       output = models.FloatField()
> 
> class InputForm(ModelForm):
>     class Meta :
>         model = Input
> 
> class OutputForm(ModelForm):
>     class Meta :
>         model = Output
> 
> 
> 
> View.py
> from django.http import HttpResponseRedirect
> from django.shortcuts import render_to_response, get_object_or_404
> from mysite.add_db.models import *
> from django.template import RequestContext
> from django.core.urlresolvers import reverse
> 
> 
> def add_db(request):
>     if request.method == 'POST':
>         form = InputForm(request.POST)
>         if form.is_valid():
>             cd = form.cleaned_data
>             input1 = cd['input1']
>             input2 = cd['input2']
>             form.save()
>           form_output = OutputForm()
>           output = input1 + input2
>           p = get_object_or_404(Input, pk=max(id))
>         form_output_final = p.output_set.create(output=output)
>             return render_to_response('add_db/output.html', {'form':
> form, 'input1':input1, 'input2':input2, 'output':output},
> context_instance=RequestContext(request))
>     else:
>         form = InputForm()
>     return render_to_response('add_db/add.html', {'form': form},
> context_instance=RequestContext(request))
> 
> 
> I want to get the latest or maximum  "Id" value of INPUT table from
> database.
> 
> please help
> 
I'm not fully up to speed on the aggregate stuff, but I *think* what you
are trying to do is

    from django.db.models import Max
    maxid = Input.objects.aggregate(Max('id'))
    p = Input.objects.get(pk=maxid)

No need to worry about the possibility of an exception, since the maxid
value is guaranteed to exist (as long as there's at least one row in the
table). This does, however, rely on the assumption that auto-increment
primary keys will be monotonically increasing.

Given that the ModelForm save() method actually returns the Input object
it has just created, wouldn't you be better simply saying

    p = form.save()

and throwing away the code that does the database searching? Or have I
misunderstood the purpose of your code?

regards
 Steve
-- 
DjangoCon US 2010 September 7-9 http://djangocon.us/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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