Re: Why INSERT instead of UPDATE?

2010-11-08 Thread Torbjörn Lundquist
SOLVED!

It was a typo, the action attribute was pointing to the wrong view. Thanks a
lot for the help!

/Torbjörn


2010/11/8 Knut Ivar Nesheim 

> Look at the HTML for the form. The 'action' attribute tells the
> browser where to submit the post. The view responsible for that URL is
> the one you should be looking at.
>
> If you have a ModelForm with no instance, Django will insert a new
> row. If there is an instance and you call form.save(), it will be
> updated (unless you have mucked about with model.pk or are forcing an
> insert)
>
> And to answer the "return to" question. Whenever you do:
>
>return HttpResponseRedirect('/url/')
>
> it will appear as a completely new request to '/url/'. No magic going on
> here.
>
> Where it can get confusing is if you are using the 'reverse' or
> 'redirect' functions, which will do a reverse lookup of urls or views
> based on url name or method name. If you have multiple urls with the
> same name and arguments, for instance, you can run into problems.
>
>
> On Mon, Nov 8, 2010 at 9:48 PM, Torbjörn Lundquist
>  wrote:
> > That did not help.
> > I think I know what the problem is but I don't know how to solve it:
> > It seems that when I press the Submit button I jump to another
> view-function
> > (that inserts a new course). So, I use the same form i two different
> places,
> > both for insert and update. How does django know which view-function it
> > should "return" to?
> > /Torbjörn
> > 2010/11/7 cootetom 
> >>
> >> This is a tough one, everything looks okay. I would try removing the
> >> categories variable from your model class just to see if that is
> >> causing problems. By the way, you can get that categories list from an
> >> instance of the Course class in the following way
> >> c.coursecategories_set.all() presuming CourseCategory has a foreign
> >> key back to Course and c = a Course instance.
> >>
> >> The other thing to check is the POST. Perhaps if it contains a key
> >> named "id" it would cause an insert, just a guess though.
> >>
> >>
> >>
> >> On Nov 7, 7:09 pm, Torbjorn  wrote:
> >> > Here is the model:
> >> >
> >> > class Course(models.Model):
> >> > title = models.CharField(max_length=255)
> >> > owner = models.ForeignKey(User)
> >> >
> >> > def __unicode__(self):
> >> > return self.title
> >> >
> >> > def _getCategories(self):
> >> > cc = CourseCategories.objects.filter(course=self)
> >> > return cc
> >> >
> >> > categories = property(_getCategories)
> >> >
> >> > and the form:
> >> >
> >> > class PartialCourseForm(ModelForm):
> >> > class Meta:
> >> > model = Course
> >> > exclude = ('owner')
> >> >
> >> > Thanks
> >> > /Torbjörn
> >> >
> >> > On 7 Nov, 16:34, Marc Aymerich  wrote:
> >> >
> >> >
> >> >
> >> >
> >> >
> >> >
> >> >
> >> > > On Sun, Nov 7, 2010 at 4:21 PM, Torbjorn
> >> >
> >> > >  wrote:
> >> > > > Hi, I want to update a record but somehow it becomes an INSERT
> >> > > > instead. This is my code:
> >> >
> >> > > > def editcourse(request, course_id):
> >> > > >course= Course.objects.get(id=course_id)
> >> > > >if request.method == 'POST':
> >> > > >form = PartialCourseForm(request.POST, instance=course)
> >> > > >if form.is_valid():
> >> > > >form.save()
> >> > > >return HttpResponseRedirect("/courses/")
> >> > > >else:
> >> > > >form = PartialCourseForm(instance=course)
> >> >
> >> > > >return render_to_response("editcourse.html", {'form':form})
> >> >
> >> > > > What do I wrong?
> >> >
> >> > > Can you paste your course model?
> >> >
> >> > > --
> >> > > Marc
> >>
> >> --
> >> 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.
> >>
> >
> > --
> > 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.
> >
>
> --
> 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
> .
> 

Re: Why INSERT instead of UPDATE?

2010-11-08 Thread Knut Ivar Nesheim
Look at the HTML for the form. The 'action' attribute tells the
browser where to submit the post. The view responsible for that URL is
the one you should be looking at.

If you have a ModelForm with no instance, Django will insert a new
row. If there is an instance and you call form.save(), it will be
updated (unless you have mucked about with model.pk or are forcing an
insert)

And to answer the "return to" question. Whenever you do:

return HttpResponseRedirect('/url/')

it will appear as a completely new request to '/url/'. No magic going on here.

Where it can get confusing is if you are using the 'reverse' or
'redirect' functions, which will do a reverse lookup of urls or views
based on url name or method name. If you have multiple urls with the
same name and arguments, for instance, you can run into problems.


On Mon, Nov 8, 2010 at 9:48 PM, Torbjörn Lundquist
 wrote:
> That did not help.
> I think I know what the problem is but I don't know how to solve it:
> It seems that when I press the Submit button I jump to another view-function
> (that inserts a new course). So, I use the same form i two different places,
> both for insert and update. How does django know which view-function it
> should "return" to?
> /Torbjörn
> 2010/11/7 cootetom 
>>
>> This is a tough one, everything looks okay. I would try removing the
>> categories variable from your model class just to see if that is
>> causing problems. By the way, you can get that categories list from an
>> instance of the Course class in the following way
>> c.coursecategories_set.all() presuming CourseCategory has a foreign
>> key back to Course and c = a Course instance.
>>
>> The other thing to check is the POST. Perhaps if it contains a key
>> named "id" it would cause an insert, just a guess though.
>>
>>
>>
>> On Nov 7, 7:09 pm, Torbjorn  wrote:
>> > Here is the model:
>> >
>> > class Course(models.Model):
>> >     title = models.CharField(max_length=255)
>> >     owner = models.ForeignKey(User)
>> >
>> >     def __unicode__(self):
>> >         return self.title
>> >
>> >     def _getCategories(self):
>> >         cc = CourseCategories.objects.filter(course=self)
>> >         return cc
>> >
>> >     categories = property(_getCategories)
>> >
>> > and the form:
>> >
>> > class PartialCourseForm(ModelForm):
>> >     class Meta:
>> >         model = Course
>> >         exclude = ('owner')
>> >
>> > Thanks
>> > /Torbjörn
>> >
>> > On 7 Nov, 16:34, Marc Aymerich  wrote:
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > > On Sun, Nov 7, 2010 at 4:21 PM, Torbjorn
>> >
>> > >  wrote:
>> > > > Hi, I want to update a record but somehow it becomes an INSERT
>> > > > instead. This is my code:
>> >
>> > > > def editcourse(request, course_id):
>> > > >    course= Course.objects.get(id=course_id)
>> > > >    if request.method == 'POST':
>> > > >        form = PartialCourseForm(request.POST, instance=course)
>> > > >        if form.is_valid():
>> > > >            form.save()
>> > > >            return HttpResponseRedirect("/courses/")
>> > > >    else:
>> > > >        form = PartialCourseForm(instance=course)
>> >
>> > > >    return render_to_response("editcourse.html", {'form':form})
>> >
>> > > > What do I wrong?
>> >
>> > > Can you paste your course model?
>> >
>> > > --
>> > > Marc
>>
>> --
>> 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.
>>
>
> --
> 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.
>

-- 
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.



Re: Why INSERT instead of UPDATE?

2010-11-08 Thread Torbjörn Lundquist
That did not help.

I think I know what the problem is but I don't know how to solve it:

It seems that when I press the Submit button I jump to another view-function
(that inserts a new course). So, I use the same form i two different places,
both for insert and update. How does django know which view-function it
should "return" to?

/Torbjörn

2010/11/7 cootetom 

> This is a tough one, everything looks okay. I would try removing the
> categories variable from your model class just to see if that is
> causing problems. By the way, you can get that categories list from an
> instance of the Course class in the following way
> c.coursecategories_set.all() presuming CourseCategory has a foreign
> key back to Course and c = a Course instance.
>
> The other thing to check is the POST. Perhaps if it contains a key
> named "id" it would cause an insert, just a guess though.
>
>
>
> On Nov 7, 7:09 pm, Torbjorn  wrote:
> > Here is the model:
> >
> > class Course(models.Model):
> > title = models.CharField(max_length=255)
> > owner = models.ForeignKey(User)
> >
> > def __unicode__(self):
> > return self.title
> >
> > def _getCategories(self):
> > cc = CourseCategories.objects.filter(course=self)
> > return cc
> >
> > categories = property(_getCategories)
> >
> > and the form:
> >
> > class PartialCourseForm(ModelForm):
> > class Meta:
> > model = Course
> > exclude = ('owner')
> >
> > Thanks
> > /Torbjörn
> >
> > On 7 Nov, 16:34, Marc Aymerich  wrote:
> >
> >
> >
> >
> >
> >
> >
> > > On Sun, Nov 7, 2010 at 4:21 PM, Torbjorn
> >
> > >  wrote:
> > > > Hi, I want to update a record but somehow it becomes an INSERT
> > > > instead. This is my code:
> >
> > > > def editcourse(request, course_id):
> > > >course= Course.objects.get(id=course_id)
> > > >if request.method == 'POST':
> > > >form = PartialCourseForm(request.POST, instance=course)
> > > >if form.is_valid():
> > > >form.save()
> > > >return HttpResponseRedirect("/courses/")
> > > >else:
> > > >form = PartialCourseForm(instance=course)
> >
> > > >return render_to_response("editcourse.html", {'form':form})
> >
> > > > What do I wrong?
> >
> > > Can you paste your course model?
> >
> > > --
> > > Marc
>
> --
> 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.
>
>

-- 
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.



Re: Why INSERT instead of UPDATE?

2010-11-07 Thread cootetom
This is a tough one, everything looks okay. I would try removing the
categories variable from your model class just to see if that is
causing problems. By the way, you can get that categories list from an
instance of the Course class in the following way
c.coursecategories_set.all() presuming CourseCategory has a foreign
key back to Course and c = a Course instance.

The other thing to check is the POST. Perhaps if it contains a key
named "id" it would cause an insert, just a guess though.



On Nov 7, 7:09 pm, Torbjorn  wrote:
> Here is the model:
>
> class Course(models.Model):
>     title = models.CharField(max_length=255)
>     owner = models.ForeignKey(User)
>
>     def __unicode__(self):
>         return self.title
>
>     def _getCategories(self):
>         cc = CourseCategories.objects.filter(course=self)
>         return cc
>
>     categories = property(_getCategories)
>
> and the form:
>
> class PartialCourseForm(ModelForm):
>     class Meta:
>         model = Course
>         exclude = ('owner')
>
> Thanks
> /Torbjörn
>
> On 7 Nov, 16:34, Marc Aymerich  wrote:
>
>
>
>
>
>
>
> > On Sun, Nov 7, 2010 at 4:21 PM, Torbjorn
>
> >  wrote:
> > > Hi, I want to update a record but somehow it becomes an INSERT
> > > instead. This is my code:
>
> > > def editcourse(request, course_id):
> > >    course= Course.objects.get(id=course_id)
> > >    if request.method == 'POST':
> > >        form = PartialCourseForm(request.POST, instance=course)
> > >        if form.is_valid():
> > >            form.save()
> > >            return HttpResponseRedirect("/courses/")
> > >    else:
> > >        form = PartialCourseForm(instance=course)
>
> > >    return render_to_response("editcourse.html", {'form':form})
>
> > > What do I wrong?
>
> > Can you paste your course model?
>
> > --
> > Marc

-- 
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.



Re: Why INSERT instead of UPDATE?

2010-11-07 Thread Torbjorn
Here is the model:

class Course(models.Model):
title = models.CharField(max_length=255)
owner = models.ForeignKey(User)

def __unicode__(self):
return self.title

def _getCategories(self):
cc = CourseCategories.objects.filter(course=self)
return cc

categories = property(_getCategories)


and the form:

class PartialCourseForm(ModelForm):
class Meta:
model = Course
exclude = ('owner')


Thanks
/Torbjörn

On 7 Nov, 16:34, Marc Aymerich  wrote:
> On Sun, Nov 7, 2010 at 4:21 PM, Torbjorn
>
>
>
>
>
>
>
>
>
>  wrote:
> > Hi, I want to update a record but somehow it becomes an INSERT
> > instead. This is my code:
>
> > def editcourse(request, course_id):
> >    course= Course.objects.get(id=course_id)
> >    if request.method == 'POST':
> >        form = PartialCourseForm(request.POST, instance=course)
> >        if form.is_valid():
> >            form.save()
> >            return HttpResponseRedirect("/courses/")
> >    else:
> >        form = PartialCourseForm(instance=course)
>
> >    return render_to_response("editcourse.html", {'form':form})
>
> > What do I wrong?
>
> Can you paste your course model?
>
> --
> Marc

-- 
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.



Re: Why INSERT instead of UPDATE?

2010-11-07 Thread Marc Aymerich
On Sun, Nov 7, 2010 at 4:21 PM, Torbjorn
 wrote:
> Hi, I want to update a record but somehow it becomes an INSERT
> instead. This is my code:
>
> def editcourse(request, course_id):
>    course= Course.objects.get(id=course_id)
>    if request.method == 'POST':
>        form = PartialCourseForm(request.POST, instance=course)
>        if form.is_valid():
>            form.save()
>            return HttpResponseRedirect("/courses/")
>    else:
>        form = PartialCourseForm(instance=course)
>
>    return render_to_response("editcourse.html", {'form':form})
>
> What do I wrong?

Can you paste your course model?


-- 
Marc

-- 
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.



Re: Why INSERT instead of UPDATE?

2010-11-07 Thread cootetom
What does your PartialCourseForm class look like? The code you have
there looks okay to me.



On Nov 7, 3:21 pm, Torbjorn  wrote:
> Hi, I want to update a record but somehow it becomes an INSERT
> instead. This is my code:
>
> def editcourse(request, course_id):
>     course= Course.objects.get(id=course_id)
>     if request.method == 'POST':
>         form = PartialCourseForm(request.POST, instance=course)
>         if form.is_valid():
>             form.save()
>             return HttpResponseRedirect("/courses/")
>     else:
>         form = PartialCourseForm(instance=course)
>
>     return render_to_response("editcourse.html", {'form':form})
>
> What do I wrong?
> Thanks
> /Torbjörn

-- 
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.



Why INSERT instead of UPDATE?

2010-11-07 Thread Torbjorn
Hi, I want to update a record but somehow it becomes an INSERT
instead. This is my code:

def editcourse(request, course_id):
course= Course.objects.get(id=course_id)
if request.method == 'POST':
form = PartialCourseForm(request.POST, instance=course)
if form.is_valid():
form.save()
return HttpResponseRedirect("/courses/")
else:
form = PartialCourseForm(instance=course)

return render_to_response("editcourse.html", {'form':form})

What do I wrong?
Thanks
/Torbjörn

-- 
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.