I have a way that works well or is to create, but this and
filter_by
. I want the same thing, but its use or, like:
def get_or_create_or (cls, session, ** kwargs): example = session.query (cls) .filter (or _ ( **
This does not work because i.e. does not expect keyword logic. How can I create a normalized version of this? For example, in a phone and an email to me May have been taken, and will return a line which corresponds to both. I am in a phone and an email. I want to get a line and get a line that matches any match.
Your problem It does not expect that the keyword logic is not a sensory thing to pass in the first place filter ()
, And _ ()
and or _ ()
all expect one or more that define the overall expression (in fact the filter ()
Simply put a and _ (* args)
in your own * arguments
in the default and 'underlying'). You must be signed in:
def get_or_create_or (cls, session, * args): example = session.kudi (cls) .filter (or _ (* args)). First () # of the rest of the stuff
and it should be called in such a way:
& gt; & Gt; & Gt; Your confusion may be by using filter_by
, which is
only comparisons of comparisons And thus the use of keyword arguments makes sense. Unfortunately, for whatever reason, there is no orring version of filter_by
you can duplicate it through:
def or_filter_by_conditions (CLS, * * Kwargs): return or_ (getattr (cls, name) = value for name, kwargs.iteritems () in value
which changes your original method like this:
def get_or_create_or (cls, session, ** kwargs): example = session.query (CLS) However, obviously you leave very expressive power, but come now You can only compare the similarity of its filtering conditions. Of course if you were doing anyway (which might be appropriate for a fluctuation), then this is not a great deal. After saying this, in general, I suggest that you have read, because there are many potentially huge passes, the way you go beyond the simple requirement to correct your query conditions Trying to do this. It will only do what you expect that if there is only one user, you are working in a session, which is ever connected to your database.
No comments:
Post a Comment