1

using datamapper, if you want to "either find the first resource matching some given criteria or just create that resource if it can't be found, you can use #first_or_create."

i am using flask-sqlalchemy and am wondering if there is a similar feature.

thanks!

SeanPlusPlus
  • 8,663
  • 18
  • 59
  • 84

1 Answers1

1

there is some things in django named get_or_create you can create somethings like that for sqlalachemy

def get_or_create(session, model, **kwargs):
    instance = session.query(model).filter_by(**kwargs).first()
    if instance:
        return instance
    else:
        instance = model(**kwargs)
        return instance

from here: python - Does SQLAlchemy have an equivalent of Django's get_or_create? - Stack Overflow -> Does SQLAlchemy have an equivalent of Django's get_or_create?

Community
  • 1
  • 1
Mohammad Efazati
  • 4,812
  • 2
  • 35
  • 50