Re: Update a single column of a row in a Model
Many thanks Karen. This worked perfectly (though the 'name' attribute is just name without the quotes). Cheers, Ken On 5 Mar, 01:42, Karen Tracey wrote: > On Thu, Mar 4, 2010 at 2:24 PM, Ken wrote: > > Thanks for your example, but whilst you're correct about Person.id not > > getting updated, all the other columns do get changed (even if it is > > to the same value). > > > Here's my code... > > > class TcsDetectionListsForm(forms.Form): > > name = forms.CharField() > > > def candidateWithForm(request, tcs_transient_objects_id): > > detectionListRow = TcsDetectionLists.objects.get(pk=0) > > if request.method == 'POST': > > > > > > > form = TcsDetectionListsForm(request.POST) > > if form.is_valid(): # All validation rules pass > > detectionListRow.name = form.cleaned_data['name'] > > detectionListRow.save() > > else: > > form = TcsDetectionListsForm(initial={'name': > > detectionListRow.name }) > > > Here's what happened in the database (from the DB log): > > > 223592 Query UPDATE `tcs_detection_lists` SET `name` = 'rubbish', > > `description` = 'Bad Candidates' WHERE `tcs_detection_lists`.`id` = 0 > > > We shouldn't be updating the 'description' column. If my security > > settings were in place, this query would fail, because my DB user only > > has update access to the 'name' column. > > TcsDetectionLists.objects.filter(pk=0).update('name'=form.cleaned_data['nam > e']) > > will produce an SQL UPDATE that only touches the name column. > > Karen -- 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: Update a single column of a row in a Model
On Thu, Mar 4, 2010 at 2:24 PM, Ken wrote: > Thanks for your example, but whilst you're correct about Person.id not > getting updated, all the other columns do get changed (even if it is > to the same value). > > Here's my code... > > class TcsDetectionListsForm(forms.Form): >name = forms.CharField() > > def candidateWithForm(request, tcs_transient_objects_id): >detectionListRow = TcsDetectionLists.objects.get(pk=0) > > if request.method == 'POST': >form = TcsDetectionListsForm(request.POST) >if form.is_valid(): # All validation rules pass >detectionListRow.name = form.cleaned_data['name'] >detectionListRow.save() >else: >form = TcsDetectionListsForm(initial={'name': > detectionListRow.name }) > > Here's what happened in the database (from the DB log): > > 223592 QueryUPDATE `tcs_detection_lists` SET `name` = 'rubbish', > `description` = 'Bad Candidates' WHERE `tcs_detection_lists`.`id` = 0 > > We shouldn't be updating the 'description' column. If my security > settings were in place, this query would fail, because my DB user only > has update access to the 'name' column. > TcsDetectionLists.objects.filter(pk=0).update('name'=form.cleaned_data['name']) will produce an SQL UPDATE that only touches the name column. Karen -- 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: Update a single column of a row in a Model
Thanks for your example, but whilst you're correct about Person.id not getting updated, all the other columns do get changed (even if it is to the same value). Here's my code... class TcsDetectionListsForm(forms.Form): name = forms.CharField() def candidateWithForm(request, tcs_transient_objects_id): detectionListRow = TcsDetectionLists.objects.get(pk=0) if request.method == 'POST': form = TcsDetectionListsForm(request.POST) if form.is_valid(): # All validation rules pass detectionListRow.name = form.cleaned_data['name'] detectionListRow.save() else: form = TcsDetectionListsForm(initial={'name': detectionListRow.name }) Here's what happened in the database (from the DB log): 223592 QueryUPDATE `tcs_detection_lists` SET `name` = 'rubbish', `description` = 'Bad Candidates' WHERE `tcs_detection_lists`.`id` = 0 We shouldn't be updating the 'description' column. If my security settings were in place, this query would fail, because my DB user only has update access to the 'name' column. Ken On 4 Mar, 18:35, Shawn Milochik wrote: > 1. Doing Person.save() will NOT update every field in your model. > > 2. Your snippet uses a ModelForm. Mine used a Form. There's a huge difference. > If you use a ModelForm you're going to have to exclude all the fields you > don't want. > > 3. If you use a ModelForm and instantiate it with request.POST, don't send an > argument > for 'instance' as well. One or the other. > > Look at my example again. It does exactly what you want. > It seems that all your problems are due to not understanding the difference > between a Form and a ModelForm. > > Read the Django book ("The Definitive Guide"). > > Shawn > > On Mar 4, 2010, at 1:29 PM, Ken wrote: > > > > > Thanks Shawn > > > My problem is that Person.save() will do an update of all my columns. > > Even though they are all identical, apart from the changed value, this > > will violate my minimum privileges requirement of only allowing the > > application access to the columns that it is allowed to change - hence > > your original suggestion of using ModelForms (and only specified > > fields). > > > Maybe if I try something like this... > > > #Use a ModelForm for Person.. > > class PersonForm(forms.ModelForm): > > model=Person > > fields = ('age') > > > #When the user submits the form: > > person = Person.objects.get(pk = 123) > > person_form = PersonForm(request.POST, instance = person) > > if person_form.is_valid() > > person_form.save() > > > Would that work...? I'll attempt to hack an example in my code and > > tell you what happens... > > > Thanks for your time... > > > Ken > > > On 4 Mar, 17:28, Shawn Milochik wrote: > >> Here's a simple example. It could be improved, but it's meant to be very > >> simple. > > >> #Make a simple form. > > >> class AgeForm(forms.Form): > >> age = forms.IntegerField() > > >> #When the user submits the form: > > >> age_form = AgeForm(request.POST) > > >> if age_form.is_valid() > > >> #get the pk however you need to > >> person = Person.objects.get(pk = 123) > >> person.age = age_form.cleaned_data['age'] > >> person.save() > > > -- > > 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 > > athttp://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: Update a single column of a row in a Model
1. Doing Person.save() will NOT update every field in your model. 2. Your snippet uses a ModelForm. Mine used a Form. There's a huge difference. If you use a ModelForm you're going to have to exclude all the fields you don't want. 3. If you use a ModelForm and instantiate it with request.POST, don't send an argument for 'instance' as well. One or the other. Look at my example again. It does exactly what you want. It seems that all your problems are due to not understanding the difference between a Form and a ModelForm. Read the Django book ("The Definitive Guide"). Shawn On Mar 4, 2010, at 1:29 PM, Ken wrote: > Thanks Shawn > > My problem is that Person.save() will do an update of all my columns. > Even though they are all identical, apart from the changed value, this > will violate my minimum privileges requirement of only allowing the > application access to the columns that it is allowed to change - hence > your original suggestion of using ModelForms (and only specified > fields). > > Maybe if I try something like this... > > > #Use a ModelForm for Person.. > class PersonForm(forms.ModelForm): >model=Person >fields = ('age') > > #When the user submits the form: > person = Person.objects.get(pk = 123) > person_form = PersonForm(request.POST, instance = person) > if person_form.is_valid() >person_form.save() > > Would that work...? I'll attempt to hack an example in my code and > tell you what happens... > > Thanks for your time... > > Ken > > > On 4 Mar, 17:28, Shawn Milochik wrote: >> Here's a simple example. It could be improved, but it's meant to be very >> simple. >> >> #Make a simple form. >> >> class AgeForm(forms.Form): >> age = forms.IntegerField() >> >> #When the user submits the form: >> >> age_form = AgeForm(request.POST) >> >> if age_form.is_valid() >> >> #get the pk however you need to >> person = Person.objects.get(pk = 123) >> person.age = age_form.cleaned_data['age'] >> person.save() > > -- > 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: Update a single column of a row in a Model
Thanks Shawn My problem is that Person.save() will do an update of all my columns. Even though they are all identical, apart from the changed value, this will violate my minimum privileges requirement of only allowing the application access to the columns that it is allowed to change - hence your original suggestion of using ModelForms (and only specified fields). Maybe if I try something like this... #Use a ModelForm for Person.. class PersonForm(forms.ModelForm): model=Person fields = ('age') #When the user submits the form: person = Person.objects.get(pk = 123) person_form = PersonForm(request.POST, instance = person) if person_form.is_valid() person_form.save() Would that work...? I'll attempt to hack an example in my code and tell you what happens... Thanks for your time... Ken On 4 Mar, 17:28, Shawn Milochik wrote: > Here's a simple example. It could be improved, but it's meant to be very > simple. > > #Make a simple form. > > class AgeForm(forms.Form): > age = forms.IntegerField() > > #When the user submits the form: > > age_form = AgeForm(request.POST) > > if age_form.is_valid() > > #get the pk however you need to > person = Person.objects.get(pk = 123) > person.age = age_form.cleaned_data['age'] > person.save() -- 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: Update a single column of a row in a Model
Here's a simple example. It could be improved, but it's meant to be very simple. #Make a simple form. class AgeForm(forms.Form): age = forms.IntegerField() #When the user submits the form: age_form = AgeForm(request.POST) if age_form.is_valid() #get the pk however you need to person = Person.objects.get(pk = 123) person.age = age_form.cleaned_data['age'] person.save() -- 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: Update a single column of a row in a Model
Thanks Unfortunately I'm very new to Forms/ModelForms, and I'm having a lot of difficulty understanding the Django examples. All I want is to allow my users to make one change to one column. I don't want them to see the ID, just a text box and a submit button. Hitting Submit should take them back to exactly the same page, with the text field pre-populated to what they entered. The database update should only change one column of the relevant row in the database table. This would take 30 seconds to do in PHP. Can anyone point me to some simple examples (other than the Django site). Cheers, Ken On 3 Mar, 00:31, Shawn Milochik wrote: > Use a forms.ModelForm and exclude all the fields except one, or use a > forms.Form with one field and use its value in a queryset .update() > call. > > Shawn > > Sent from my iPhone > > On Mar 2, 2010, at 7:11 PM, Ken wrote: > > > > > Folks > > > This is a bit of a newbie question on Model updates. I've been > > happily using Django to give my users a read-only view of the database > > for nearly a year, but now I'd like my users to be able to update a > > row in one of my tables. > > > We're told in the Model documentation that "Django knows best" when it > > comes to talking to the database. > > > However, I want my users to ONLY update ONE column in one row in my > > database, but Django seems to insist on updating all the columns. (A > > related problem is that I'd also like the database user only to be > > allowed to update a subset of columns of my table - mostly because I'm > > paranoid.) > > > If you don't specify the column value, and the column is nullable, the > > unspecified columns get set to NULL instead of left alone. > > > Here's an example... > > > select * from tcs_detection_lists; > > ++---+-+ > > | id | name | description | > > ++---+-+ > > | 0 | garbage | Bad Candidates | > > | 1 | confirmed | Confirmed SNe | > > | 2 | good | Good Candidates | > > | 3 | possible | Possible Candidates | > > | 4 | pending | Not Yet Eyeballed | > > ++---+-+ > > > I want to update the word 'garbage' to 'bad'. > > > Here's my simplistic Django code... > > > dl = views.TcsDetectionLists(id=0, name ='bad') > > dl.save() > > > select * from tcs_detection_lists; > > ++---+-+ > > | id | name | description | > > ++---+-+ > > | 0 | bad | | > > | 1 | confirmed | Confirmed SNe | > > | 2 | good | Good Candidates | > > | 3 | possible | Possible Candidates | > > | 4 | pending | Not Yet Eyeballed | > > ++---+-+ > > > I only wanted the user to change one column, but Django has assumed > > None for the rest of the columns and therefore deleted my > > 'description' field. > > > The update that I want is: > > > update tcs_detection_lists > > set name = 'bad' > > where id = 0; > > > Note that some of my tables have many columns (>50) with a lot of > > floating point data and I certainly don't want my users inadvertently > > deleting data. > > > Do I need to specify something in my Model columns to get Django to > > behave the way I want it to...? I really don't want to have to resort > > to a bit of custom SQL... > > > Thanks in advance, > > > Ken > > > -- > > 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 > > athttp://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: Update a single column of a row in a Model
Use a forms.ModelForm and exclude all the fields except one, or use a forms.Form with one field and use its value in a queryset .update() call. Shawn Sent from my iPhone On Mar 2, 2010, at 7:11 PM, Ken wrote: Folks This is a bit of a newbie question on Model updates. I've been happily using Django to give my users a read-only view of the database for nearly a year, but now I'd like my users to be able to update a row in one of my tables. We're told in the Model documentation that "Django knows best" when it comes to talking to the database. However, I want my users to ONLY update ONE column in one row in my database, but Django seems to insist on updating all the columns. (A related problem is that I'd also like the database user only to be allowed to update a subset of columns of my table - mostly because I'm paranoid.) If you don't specify the column value, and the column is nullable, the unspecified columns get set to NULL instead of left alone. Here's an example... select * from tcs_detection_lists; ++---+-+ | id | name | description | ++---+-+ | 0 | garbage | Bad Candidates | | 1 | confirmed | Confirmed SNe | | 2 | good | Good Candidates | | 3 | possible | Possible Candidates | | 4 | pending | Not Yet Eyeballed | ++---+-+ I want to update the word 'garbage' to 'bad'. Here's my simplistic Django code... dl = views.TcsDetectionLists(id=0, name ='bad') dl.save() select * from tcs_detection_lists; ++---+-+ | id | name | description | ++---+-+ | 0 | bad | | | 1 | confirmed | Confirmed SNe | | 2 | good | Good Candidates | | 3 | possible | Possible Candidates | | 4 | pending | Not Yet Eyeballed | ++---+-+ I only wanted the user to change one column, but Django has assumed None for the rest of the columns and therefore deleted my 'description' field. The update that I want is: update tcs_detection_lists set name = 'bad' where id = 0; Note that some of my tables have many columns (>50) with a lot of floating point data and I certainly don't want my users inadvertently deleting data. Do I need to specify something in my Model columns to get Django to behave the way I want it to...? I really don't want to have to resort to a bit of custom SQL... Thanks in advance, Ken -- 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.
Update a single column of a row in a Model
Folks This is a bit of a newbie question on Model updates. I've been happily using Django to give my users a read-only view of the database for nearly a year, but now I'd like my users to be able to update a row in one of my tables. We're told in the Model documentation that "Django knows best" when it comes to talking to the database. However, I want my users to ONLY update ONE column in one row in my database, but Django seems to insist on updating all the columns. (A related problem is that I'd also like the database user only to be allowed to update a subset of columns of my table - mostly because I'm paranoid.) If you don't specify the column value, and the column is nullable, the unspecified columns get set to NULL instead of left alone. Here's an example... select * from tcs_detection_lists; ++---+-+ | id | name | description | ++---+-+ | 0 | garbage | Bad Candidates | | 1 | confirmed | Confirmed SNe | | 2 | good | Good Candidates | | 3 | possible | Possible Candidates | | 4 | pending | Not Yet Eyeballed | ++---+-+ I want to update the word 'garbage' to 'bad'. Here's my simplistic Django code... dl = views.TcsDetectionLists(id=0, name ='bad') dl.save() select * from tcs_detection_lists; ++---+-+ | id | name | description | ++---+-+ | 0 | bad | | | 1 | confirmed | Confirmed SNe | | 2 | good | Good Candidates | | 3 | possible | Possible Candidates | | 4 | pending | Not Yet Eyeballed | ++---+-+ I only wanted the user to change one column, but Django has assumed None for the rest of the columns and therefore deleted my 'description' field. The update that I want is: update tcs_detection_lists set name = 'bad' where id = 0; Note that some of my tables have many columns (>50) with a lot of floating point data and I certainly don't want my users inadvertently deleting data. Do I need to specify something in my Model columns to get Django to behave the way I want it to...? I really don't want to have to resort to a bit of custom SQL... Thanks in advance, Ken -- 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.