SqlAlchemy filter_by with kwargs containing listsSQLAlchemy IN clauseDynamically constructing filters in SQLAlchemyHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?Use of *args and **kwargsDoes Python have a string 'contains' substring method?

How would a disabled person earn their living in a medieval-type town?

Can authors email you PDFs of their textbook for free?

Am I required to correct my opponent's assumptions about my morph creatures?

Why are CEOs generally fired rather being demoted?

An alternative to "two column" geometry proofs

Understanding GFCI configuration in basement

How are the cards determined in an incomplete deck of many things?

Correct way of simplifying the result of an integral

How does Query decide the order in which the functions are applied?

Why do fuses burn at a specific current?

Why is Mitch McConnell blocking nominees to the Federal Election Commission?

Why wasn't Linda Hamilton in T3?

Polarity of gas discharge tubes?

How could reincarnation magic be limited to prevent overuse?

How does the search space affect the speed of an ILP solver?

Why is this reversed StringBuilder not equal to the original String, when it is a palindrome?

Received email from ISP saying one of my devices has malware

How were US credit cards verified in-store in the 1980's?

Is the equational theory of groups axiomatized by the associative law?

New coworker has strange workplace requirements - how should I deal with them?

Should we run PBKDF2 for every plaintext to be protected or should we run PBKDF2 only once?

Underbrace in equation

What happens if you just start drawing from the Deck of Many Things without declaring any number of cards?

How to solve this inequality , when there is a irrational power?



SqlAlchemy filter_by with kwargs containing lists


SQLAlchemy IN clauseDynamically constructing filters in SQLAlchemyHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?Use of *args and **kwargsDoes Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm trying to build a filter statement from kwargs that may contain lists as values:



def delete_object(self, from_table, filters, commit=True): 
(self._session
.query(from_table)
.filter_by(**filters)
.delete(synchronize_session=False))


The filters contain keys which represent columns in the table and values that are supposed to be filtered.
If the values are not lists



'id': 1, 'name': 'test'


then it works fine. However, when the values are lists



'id': [3]


then the resulting sql statement that is created looks like this



DELETE FROM <table> WHERE <table>.id = ARRAY[3]


Is there a way to handle list elements as well?










share|improve this question
























  • What about unpacking the list and loop? I'm not sure if SQLAlchemy has that level of robust to be able to parse your kwargs of different data objects

    – kerwei
    Mar 28 at 1:53











  • Yes that's an option but for lists with 100+ entries that's a lot of queries

    – wasp256
    Mar 28 at 2:53











  • No it wouldn't be creating individual queries for each item in the list, it would dynamically create a filter for each object in the list and then apply all the filters to a single query. See this answer: stackoverflow.com/a/14887813/6560549 for something similar.

    – SuperShoot
    Mar 28 at 3:01

















1















I'm trying to build a filter statement from kwargs that may contain lists as values:



def delete_object(self, from_table, filters, commit=True): 
(self._session
.query(from_table)
.filter_by(**filters)
.delete(synchronize_session=False))


The filters contain keys which represent columns in the table and values that are supposed to be filtered.
If the values are not lists



'id': 1, 'name': 'test'


then it works fine. However, when the values are lists



'id': [3]


then the resulting sql statement that is created looks like this



DELETE FROM <table> WHERE <table>.id = ARRAY[3]


Is there a way to handle list elements as well?










share|improve this question
























  • What about unpacking the list and loop? I'm not sure if SQLAlchemy has that level of robust to be able to parse your kwargs of different data objects

    – kerwei
    Mar 28 at 1:53











  • Yes that's an option but for lists with 100+ entries that's a lot of queries

    – wasp256
    Mar 28 at 2:53











  • No it wouldn't be creating individual queries for each item in the list, it would dynamically create a filter for each object in the list and then apply all the filters to a single query. See this answer: stackoverflow.com/a/14887813/6560549 for something similar.

    – SuperShoot
    Mar 28 at 3:01













1












1








1








I'm trying to build a filter statement from kwargs that may contain lists as values:



def delete_object(self, from_table, filters, commit=True): 
(self._session
.query(from_table)
.filter_by(**filters)
.delete(synchronize_session=False))


The filters contain keys which represent columns in the table and values that are supposed to be filtered.
If the values are not lists



'id': 1, 'name': 'test'


then it works fine. However, when the values are lists



'id': [3]


then the resulting sql statement that is created looks like this



DELETE FROM <table> WHERE <table>.id = ARRAY[3]


Is there a way to handle list elements as well?










share|improve this question














I'm trying to build a filter statement from kwargs that may contain lists as values:



def delete_object(self, from_table, filters, commit=True): 
(self._session
.query(from_table)
.filter_by(**filters)
.delete(synchronize_session=False))


The filters contain keys which represent columns in the table and values that are supposed to be filtered.
If the values are not lists



'id': 1, 'name': 'test'


then it works fine. However, when the values are lists



'id': [3]


then the resulting sql statement that is created looks like this



DELETE FROM <table> WHERE <table>.id = ARRAY[3]


Is there a way to handle list elements as well?







python sqlalchemy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 0:30









wasp256wasp256

2,2024 gold badges35 silver badges72 bronze badges




2,2024 gold badges35 silver badges72 bronze badges















  • What about unpacking the list and loop? I'm not sure if SQLAlchemy has that level of robust to be able to parse your kwargs of different data objects

    – kerwei
    Mar 28 at 1:53











  • Yes that's an option but for lists with 100+ entries that's a lot of queries

    – wasp256
    Mar 28 at 2:53











  • No it wouldn't be creating individual queries for each item in the list, it would dynamically create a filter for each object in the list and then apply all the filters to a single query. See this answer: stackoverflow.com/a/14887813/6560549 for something similar.

    – SuperShoot
    Mar 28 at 3:01

















  • What about unpacking the list and loop? I'm not sure if SQLAlchemy has that level of robust to be able to parse your kwargs of different data objects

    – kerwei
    Mar 28 at 1:53











  • Yes that's an option but for lists with 100+ entries that's a lot of queries

    – wasp256
    Mar 28 at 2:53











  • No it wouldn't be creating individual queries for each item in the list, it would dynamically create a filter for each object in the list and then apply all the filters to a single query. See this answer: stackoverflow.com/a/14887813/6560549 for something similar.

    – SuperShoot
    Mar 28 at 3:01
















What about unpacking the list and loop? I'm not sure if SQLAlchemy has that level of robust to be able to parse your kwargs of different data objects

– kerwei
Mar 28 at 1:53





What about unpacking the list and loop? I'm not sure if SQLAlchemy has that level of robust to be able to parse your kwargs of different data objects

– kerwei
Mar 28 at 1:53













Yes that's an option but for lists with 100+ entries that's a lot of queries

– wasp256
Mar 28 at 2:53





Yes that's an option but for lists with 100+ entries that's a lot of queries

– wasp256
Mar 28 at 2:53













No it wouldn't be creating individual queries for each item in the list, it would dynamically create a filter for each object in the list and then apply all the filters to a single query. See this answer: stackoverflow.com/a/14887813/6560549 for something similar.

– SuperShoot
Mar 28 at 3:01





No it wouldn't be creating individual queries for each item in the list, it would dynamically create a filter for each object in the list and then apply all the filters to a single query. See this answer: stackoverflow.com/a/14887813/6560549 for something similar.

– SuperShoot
Mar 28 at 3:01












1 Answer
1






active

oldest

votes


















0















It looks like this answer may be able to help:



SQLAlchemy IN clause



Summing is up, you can us _in, like so:



session.query(MyUserClass)
.filter(MyUserClass.id.in_(SOME_LIST)).all()





share|improve this answer

























  • Yes I know about the in_ but that does not really relate to my code since it takes away the dynamic part and me having to parse all kwargs manually...

    – wasp256
    Mar 28 at 0:38










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388463%2fsqlalchemy-filter-by-with-kwargs-containing-lists%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















It looks like this answer may be able to help:



SQLAlchemy IN clause



Summing is up, you can us _in, like so:



session.query(MyUserClass)
.filter(MyUserClass.id.in_(SOME_LIST)).all()





share|improve this answer

























  • Yes I know about the in_ but that does not really relate to my code since it takes away the dynamic part and me having to parse all kwargs manually...

    – wasp256
    Mar 28 at 0:38















0















It looks like this answer may be able to help:



SQLAlchemy IN clause



Summing is up, you can us _in, like so:



session.query(MyUserClass)
.filter(MyUserClass.id.in_(SOME_LIST)).all()





share|improve this answer

























  • Yes I know about the in_ but that does not really relate to my code since it takes away the dynamic part and me having to parse all kwargs manually...

    – wasp256
    Mar 28 at 0:38













0














0










0









It looks like this answer may be able to help:



SQLAlchemy IN clause



Summing is up, you can us _in, like so:



session.query(MyUserClass)
.filter(MyUserClass.id.in_(SOME_LIST)).all()





share|improve this answer













It looks like this answer may be able to help:



SQLAlchemy IN clause



Summing is up, you can us _in, like so:



session.query(MyUserClass)
.filter(MyUserClass.id.in_(SOME_LIST)).all()






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 0:34









Matthew Salvatore ViglioneMatthew Salvatore Viglione

6652 silver badges21 bronze badges




6652 silver badges21 bronze badges















  • Yes I know about the in_ but that does not really relate to my code since it takes away the dynamic part and me having to parse all kwargs manually...

    – wasp256
    Mar 28 at 0:38

















  • Yes I know about the in_ but that does not really relate to my code since it takes away the dynamic part and me having to parse all kwargs manually...

    – wasp256
    Mar 28 at 0:38
















Yes I know about the in_ but that does not really relate to my code since it takes away the dynamic part and me having to parse all kwargs manually...

– wasp256
Mar 28 at 0:38





Yes I know about the in_ but that does not really relate to my code since it takes away the dynamic part and me having to parse all kwargs manually...

– wasp256
Mar 28 at 0:38








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388463%2fsqlalchemy-filter-by-with-kwargs-containing-lists%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript