What is the equivalent of Django s get or create om SQLAlchemy using Python

0 votes

Hi all. My requirement was that I needed to get my hands on an object from a database (if it exists) or if not, to create it on my own.

Django has a method called get_or_create and it does exactly this. But, my question is that, is there an alternative or an equivalent to use the same thing to obtain same functionality in SWLAlchemy?

This is what I am doing now:

def get_or_create_instrument(session, serial_number):
    instrument = session.query(Instrument).filter_by(serial_number=serial_number).first()
    if instrument:
        return instrument
    else:
        instrument = Instrument(serial_number)
        session.add(instrument)
        return instrument

All help appreciated!

Feb 14, 2019 in Python by Anirudh
• 2,080 points
3,250 views

1 answer to this question.

0 votes

As far as I know, there is no literal shortcut to go about doing this. And whatever you're doing is the right way to go about doing it.

You can also generalize it and go about it this way, check it out:

def get_or_create(session, model, defaults=None, **kwargs):
    instance = session.query(model).filter_by(**kwargs).first()
    if instance:
        return instance, False
    else:
        params = dict((k, v) for k, v in kwargs.iteritems() if not isinstance(v, ClauseElement))
        params.update(defaults or {})
        instance = model(**params)
        session.add(instance)
        return instance, True

Hope this helped!

answered Feb 14, 2019 by Nymeria
• 3,560 points

Related Questions In Python

0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11, 2019 in Python by Nisa
• 1,090 points
1,429 views
0 votes
1 answer

What is the Python 3 equivalent of “python -m SimpleHTTPServer”

The SimpleHTTPServer module has been merged into http.server in Python 3.0. ...READ MORE

answered Nov 28, 2020 in Python by Gitika
• 65,910 points
468 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
4,293 views
0 votes
2 answers

What is the use of Python language?

python is general purpose programming language.it very ...READ MORE

answered Mar 15, 2019 in Python by rajesh kumar
686 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,060 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,480 views
0 votes
1 answer

What is the equivalent of NotImplementedError using Python?

Hi, good question! One simple answer to your ...READ MORE

answered Jan 17, 2019 in Python by Nymeria
• 3,560 points
2,048 views
0 votes
1 answer

What is the recommended way to randomize a list of strings using Python?

Hi. Nice question. Here is the simplified answer ...READ MORE

answered Jan 18, 2019 in Python by Nymeria
• 3,560 points
614 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP