Saving multiple column values using a for loop in Django
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
In my database (SQLite), I would like to update every column in a given row by using a for loop through a dictionary. I've searched both SO and Django documentation, but have been unable to find any information regarding updating multiple columns via a for loop. I suspect that there's an easy solution to my problem, but I've run out of keywords to google.
Here's a very simple example that boils the problem down to its core:
models.py
from django.db import models
class author_ratings(models.Model):
name = models.TextField()
book_one = models.IntegerField()
book_two = models.IntegerField()
book_three = models.IntegerField()
views.py
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
for book,rating in ratings_dictionary:
current_author.book = rating
current_author.save()
The problem: Instead of saving the values of 10, 20, and 30 into the corresponding columns of this author's row, Django throws the following error:
"AttributeError: 'current_author' object has no attribute 'book'"
EDIT:
In the answers below, Shafikur Rahman correctly points out that 'book' is not an available field (only "name", "book_one", "book_two", and "book_three" are available). My question becomes: how do you use a for loop to update every field without having to hardcode all of them?
For example, I want to avoid having to explicitly type out every column:
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
The above information should be enough to fully understand the question, but I can provide any other details upon request.
Thank you in advance for any assistance.
django python-3.x django-models django-views
add a comment |
In my database (SQLite), I would like to update every column in a given row by using a for loop through a dictionary. I've searched both SO and Django documentation, but have been unable to find any information regarding updating multiple columns via a for loop. I suspect that there's an easy solution to my problem, but I've run out of keywords to google.
Here's a very simple example that boils the problem down to its core:
models.py
from django.db import models
class author_ratings(models.Model):
name = models.TextField()
book_one = models.IntegerField()
book_two = models.IntegerField()
book_three = models.IntegerField()
views.py
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
for book,rating in ratings_dictionary:
current_author.book = rating
current_author.save()
The problem: Instead of saving the values of 10, 20, and 30 into the corresponding columns of this author's row, Django throws the following error:
"AttributeError: 'current_author' object has no attribute 'book'"
EDIT:
In the answers below, Shafikur Rahman correctly points out that 'book' is not an available field (only "name", "book_one", "book_two", and "book_three" are available). My question becomes: how do you use a for loop to update every field without having to hardcode all of them?
For example, I want to avoid having to explicitly type out every column:
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
The above information should be enough to fully understand the question, but I can provide any other details upon request.
Thank you in advance for any assistance.
django python-3.x django-models django-views
add a comment |
In my database (SQLite), I would like to update every column in a given row by using a for loop through a dictionary. I've searched both SO and Django documentation, but have been unable to find any information regarding updating multiple columns via a for loop. I suspect that there's an easy solution to my problem, but I've run out of keywords to google.
Here's a very simple example that boils the problem down to its core:
models.py
from django.db import models
class author_ratings(models.Model):
name = models.TextField()
book_one = models.IntegerField()
book_two = models.IntegerField()
book_three = models.IntegerField()
views.py
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
for book,rating in ratings_dictionary:
current_author.book = rating
current_author.save()
The problem: Instead of saving the values of 10, 20, and 30 into the corresponding columns of this author's row, Django throws the following error:
"AttributeError: 'current_author' object has no attribute 'book'"
EDIT:
In the answers below, Shafikur Rahman correctly points out that 'book' is not an available field (only "name", "book_one", "book_two", and "book_three" are available). My question becomes: how do you use a for loop to update every field without having to hardcode all of them?
For example, I want to avoid having to explicitly type out every column:
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
The above information should be enough to fully understand the question, but I can provide any other details upon request.
Thank you in advance for any assistance.
django python-3.x django-models django-views
In my database (SQLite), I would like to update every column in a given row by using a for loop through a dictionary. I've searched both SO and Django documentation, but have been unable to find any information regarding updating multiple columns via a for loop. I suspect that there's an easy solution to my problem, but I've run out of keywords to google.
Here's a very simple example that boils the problem down to its core:
models.py
from django.db import models
class author_ratings(models.Model):
name = models.TextField()
book_one = models.IntegerField()
book_two = models.IntegerField()
book_three = models.IntegerField()
views.py
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
for book,rating in ratings_dictionary:
current_author.book = rating
current_author.save()
The problem: Instead of saving the values of 10, 20, and 30 into the corresponding columns of this author's row, Django throws the following error:
"AttributeError: 'current_author' object has no attribute 'book'"
EDIT:
In the answers below, Shafikur Rahman correctly points out that 'book' is not an available field (only "name", "book_one", "book_two", and "book_three" are available). My question becomes: how do you use a for loop to update every field without having to hardcode all of them?
For example, I want to avoid having to explicitly type out every column:
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
The above information should be enough to fully understand the question, but I can provide any other details upon request.
Thank you in advance for any assistance.
django python-3.x django-models django-views
django python-3.x django-models django-views
edited Jan 4 at 15:09
Matthew5Johnson
asked Jan 4 at 14:57
Matthew5JohnsonMatthew5Johnson
536
536
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
In views.py you can:
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
current_author.update(**ratings_dictionary)
You can read about update
in the docs.
add a comment |
With current_author = author_ratings.objects.filter(name="Jim")
you get back a QuerySet
, not an instance of model. So by doing
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
you'll still get errors. 'QuerySet' object has no attribute 'save'
at least.
But querysets have the update
method, which could save you a few lines of code if you don't insist on using a loop:
current_author.update(**ratings_dictionary)
is all you need
add a comment |
Your author_ratings
model doesn't have field book
so it's through error. The available fields are name
, book_one
, book_two
, and book_three
Thanks for the response. I guess my question is: how do you use a for loop to avoid having to hard code every field? For example, if I have 100 columns that need to be updated, I'd rather not have to list all 100 fields for each author.
– Matthew5Johnson
Jan 4 at 15:03
Ok. Something I need to clear. Can you give an idea about your` ratings_dictionary`?
– shafik
Jan 4 at 15:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041367%2fsaving-multiple-column-values-using-a-for-loop-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In views.py you can:
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
current_author.update(**ratings_dictionary)
You can read about update
in the docs.
add a comment |
In views.py you can:
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
current_author.update(**ratings_dictionary)
You can read about update
in the docs.
add a comment |
In views.py you can:
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
current_author.update(**ratings_dictionary)
You can read about update
in the docs.
In views.py you can:
from models import author_ratings
ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}
current_author = author_ratings.objects.filter(name="Jim")
current_author.update(**ratings_dictionary)
You can read about update
in the docs.
answered Jan 4 at 15:32
raratiruraratiru
3,23022360
3,23022360
add a comment |
add a comment |
With current_author = author_ratings.objects.filter(name="Jim")
you get back a QuerySet
, not an instance of model. So by doing
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
you'll still get errors. 'QuerySet' object has no attribute 'save'
at least.
But querysets have the update
method, which could save you a few lines of code if you don't insist on using a loop:
current_author.update(**ratings_dictionary)
is all you need
add a comment |
With current_author = author_ratings.objects.filter(name="Jim")
you get back a QuerySet
, not an instance of model. So by doing
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
you'll still get errors. 'QuerySet' object has no attribute 'save'
at least.
But querysets have the update
method, which could save you a few lines of code if you don't insist on using a loop:
current_author.update(**ratings_dictionary)
is all you need
add a comment |
With current_author = author_ratings.objects.filter(name="Jim")
you get back a QuerySet
, not an instance of model. So by doing
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
you'll still get errors. 'QuerySet' object has no attribute 'save'
at least.
But querysets have the update
method, which could save you a few lines of code if you don't insist on using a loop:
current_author.update(**ratings_dictionary)
is all you need
With current_author = author_ratings.objects.filter(name="Jim")
you get back a QuerySet
, not an instance of model. So by doing
current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
you'll still get errors. 'QuerySet' object has no attribute 'save'
at least.
But querysets have the update
method, which could save you a few lines of code if you don't insist on using a loop:
current_author.update(**ratings_dictionary)
is all you need
answered Jan 4 at 15:33
4140tm4140tm
1,166714
1,166714
add a comment |
add a comment |
Your author_ratings
model doesn't have field book
so it's through error. The available fields are name
, book_one
, book_two
, and book_three
Thanks for the response. I guess my question is: how do you use a for loop to avoid having to hard code every field? For example, if I have 100 columns that need to be updated, I'd rather not have to list all 100 fields for each author.
– Matthew5Johnson
Jan 4 at 15:03
Ok. Something I need to clear. Can you give an idea about your` ratings_dictionary`?
– shafik
Jan 4 at 15:32
add a comment |
Your author_ratings
model doesn't have field book
so it's through error. The available fields are name
, book_one
, book_two
, and book_three
Thanks for the response. I guess my question is: how do you use a for loop to avoid having to hard code every field? For example, if I have 100 columns that need to be updated, I'd rather not have to list all 100 fields for each author.
– Matthew5Johnson
Jan 4 at 15:03
Ok. Something I need to clear. Can you give an idea about your` ratings_dictionary`?
– shafik
Jan 4 at 15:32
add a comment |
Your author_ratings
model doesn't have field book
so it's through error. The available fields are name
, book_one
, book_two
, and book_three
Your author_ratings
model doesn't have field book
so it's through error. The available fields are name
, book_one
, book_two
, and book_three
answered Jan 4 at 15:00
shafikshafik
2,20631330
2,20631330
Thanks for the response. I guess my question is: how do you use a for loop to avoid having to hard code every field? For example, if I have 100 columns that need to be updated, I'd rather not have to list all 100 fields for each author.
– Matthew5Johnson
Jan 4 at 15:03
Ok. Something I need to clear. Can you give an idea about your` ratings_dictionary`?
– shafik
Jan 4 at 15:32
add a comment |
Thanks for the response. I guess my question is: how do you use a for loop to avoid having to hard code every field? For example, if I have 100 columns that need to be updated, I'd rather not have to list all 100 fields for each author.
– Matthew5Johnson
Jan 4 at 15:03
Ok. Something I need to clear. Can you give an idea about your` ratings_dictionary`?
– shafik
Jan 4 at 15:32
Thanks for the response. I guess my question is: how do you use a for loop to avoid having to hard code every field? For example, if I have 100 columns that need to be updated, I'd rather not have to list all 100 fields for each author.
– Matthew5Johnson
Jan 4 at 15:03
Thanks for the response. I guess my question is: how do you use a for loop to avoid having to hard code every field? For example, if I have 100 columns that need to be updated, I'd rather not have to list all 100 fields for each author.
– Matthew5Johnson
Jan 4 at 15:03
Ok. Something I need to clear. Can you give an idea about your` ratings_dictionary`?
– shafik
Jan 4 at 15:32
Ok. Something I need to clear. Can you give an idea about your` ratings_dictionary`?
– shafik
Jan 4 at 15:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041367%2fsaving-multiple-column-values-using-a-for-loop-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown