Better approach for mass updating all User JSONfield using signalsTask for all objects in model with datetime...

Is there some relative to Dutch word "kijken" in German?

Is there any differences between "Gucken" and "Schauen"?

Why did this image turn out darker?

How to apply float precision (type specifier) in a conditional f-string?

How would a Dictatorship make a country more successful?

Why does a metal block make a shrill sound but not a wooden block upon hammering?

Compress command output by piping to bzip2

Parsing a string of key-value pairs as a dictionary

Citing paywalled articles accessed via illegal web sharing

Quenching swords in dragon blood; why?

Why is "points exist" not an axiom in geometry?

What kind of hardware implements Fourier transform?

Are there neural networks with very few nodes that decently solve non-trivial problems?

Using only 1s, make 29 with the minimum number of digits

We are very unlucky in my court

Cryptic with missing capitals

Every character has a name - does this lead to too many named characters?

How to prevent users from executing commands through browser URL

What is a jet (unit) shown in Windows 10 calculator?

Difference between thick vs thin front suspension?

If I sold a PS4 game I owned the disc for, can I reinstall it digitally?

Solving Fredholm Equation of the second kind

How would one buy a used TIE Fighter or X-Wing?

Can you combine War Caster, whip, and Warlock Features to Eldritch Blast enemies with reach?



Better approach for mass updating all User JSONfield using signals


Task for all objects in model with datetime operationsCalculator for finding massMultithreaded file downloader using threading and signalsDjango Custom Decorator for user group checkA `get_context_data` method for selecting and updating images displayed to a user over a multi page formMapping different classes to the same database tableSelecting all posts created by user that actual user followsA GUI program for a Buzzer system using arduinoGenerating combinations of specific values of a nested dictLongest Palindromic Substring better approach













-1












$begingroup$


I currently have a JSONfield on my User model that will ocasionally need to be updated, I setup a signal in which I query all users and then try to perform an update:



all_users = User.objects.all()


Initially, i thought I could update the field value by appending a new key/value to the JSONfield, however, this doesnt seem to be possible:



value_updates = all_users.values("json_data").update(json_data.myDictionary[key]= False)


so, I am iterating over all the returned values and doing so 1 by 1:
and re-querying for the user with the primary key and updating them independently as follows:



for item in values_updates:
try:
usrData = item['json_data']
usrData['key'][instance.title]=False
User.objects.filter(pk=item['pk']).update(json_data=usrData)
except KeyError as err:
print(err)


I highly doubt that my approach is optimal (and definitely not scalable) however, I have not figured out some other way to do it.



What would be the best approach in achieving this without iterating over all my users in the querySet, fetching and updating the JSONfield and then re-querying the individual users to update the field?










share|improve this question







New contributor




glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    :thinking: a downvote =/ , any feedback is aprpeciated in order to improve my post.
    $endgroup$
    – glls
    1 hour ago
















-1












$begingroup$


I currently have a JSONfield on my User model that will ocasionally need to be updated, I setup a signal in which I query all users and then try to perform an update:



all_users = User.objects.all()


Initially, i thought I could update the field value by appending a new key/value to the JSONfield, however, this doesnt seem to be possible:



value_updates = all_users.values("json_data").update(json_data.myDictionary[key]= False)


so, I am iterating over all the returned values and doing so 1 by 1:
and re-querying for the user with the primary key and updating them independently as follows:



for item in values_updates:
try:
usrData = item['json_data']
usrData['key'][instance.title]=False
User.objects.filter(pk=item['pk']).update(json_data=usrData)
except KeyError as err:
print(err)


I highly doubt that my approach is optimal (and definitely not scalable) however, I have not figured out some other way to do it.



What would be the best approach in achieving this without iterating over all my users in the querySet, fetching and updating the JSONfield and then re-querying the individual users to update the field?










share|improve this question







New contributor




glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    :thinking: a downvote =/ , any feedback is aprpeciated in order to improve my post.
    $endgroup$
    – glls
    1 hour ago














-1












-1








-1





$begingroup$


I currently have a JSONfield on my User model that will ocasionally need to be updated, I setup a signal in which I query all users and then try to perform an update:



all_users = User.objects.all()


Initially, i thought I could update the field value by appending a new key/value to the JSONfield, however, this doesnt seem to be possible:



value_updates = all_users.values("json_data").update(json_data.myDictionary[key]= False)


so, I am iterating over all the returned values and doing so 1 by 1:
and re-querying for the user with the primary key and updating them independently as follows:



for item in values_updates:
try:
usrData = item['json_data']
usrData['key'][instance.title]=False
User.objects.filter(pk=item['pk']).update(json_data=usrData)
except KeyError as err:
print(err)


I highly doubt that my approach is optimal (and definitely not scalable) however, I have not figured out some other way to do it.



What would be the best approach in achieving this without iterating over all my users in the querySet, fetching and updating the JSONfield and then re-querying the individual users to update the field?










share|improve this question







New contributor




glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I currently have a JSONfield on my User model that will ocasionally need to be updated, I setup a signal in which I query all users and then try to perform an update:



all_users = User.objects.all()


Initially, i thought I could update the field value by appending a new key/value to the JSONfield, however, this doesnt seem to be possible:



value_updates = all_users.values("json_data").update(json_data.myDictionary[key]= False)


so, I am iterating over all the returned values and doing so 1 by 1:
and re-querying for the user with the primary key and updating them independently as follows:



for item in values_updates:
try:
usrData = item['json_data']
usrData['key'][instance.title]=False
User.objects.filter(pk=item['pk']).update(json_data=usrData)
except KeyError as err:
print(err)


I highly doubt that my approach is optimal (and definitely not scalable) however, I have not figured out some other way to do it.



What would be the best approach in achieving this without iterating over all my users in the querySet, fetching and updating the JSONfield and then re-querying the individual users to update the field?







python django postgresql






share|improve this question







New contributor




glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









gllsglls

992




992




New contributor




glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






glls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    :thinking: a downvote =/ , any feedback is aprpeciated in order to improve my post.
    $endgroup$
    – glls
    1 hour ago


















  • $begingroup$
    :thinking: a downvote =/ , any feedback is aprpeciated in order to improve my post.
    $endgroup$
    – glls
    1 hour ago
















$begingroup$
:thinking: a downvote =/ , any feedback is aprpeciated in order to improve my post.
$endgroup$
– glls
1 hour ago




$begingroup$
:thinking: a downvote =/ , any feedback is aprpeciated in order to improve my post.
$endgroup$
– glls
1 hour ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});






glls is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214486%2fbetter-approach-for-mass-updating-all-user-jsonfield-using-signals%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








glls is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















glls is a new contributor. Be nice, and check out our Code of Conduct.













glls is a new contributor. Be nice, and check out our Code of Conduct.












glls is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


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


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214486%2fbetter-approach-for-mass-updating-all-user-jsonfield-using-signals%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...