0

I'm trying to write some code that checks on something, if exists then cool and if not creates it.

The problem is, where to put this code? Django doesn't recommend putting anything that deals with DB at an app's ready() function - this is what I'm currently doing but it gives me an error on debugging and give an error too on the beginning but works later on-.

Also, what I have found seems outdated and a bit hacky like putting it at urls.py or wsgi.py.

I feel there must be a better way to do it.

Thank you all.

Mahmoud Adel
  • 1,262
  • 2
  • 13
  • 23
  • Does this answer your question? [How to define default data for Django Models?](https://stackoverflow.com/questions/39739439/how-to-define-default-data-for-django-models) – Abdul Aziz Barkat Oct 14 '21 at 16:23

1 Answers1

0

Typically DB interactions within Django apps happen within the views.py file. I would suggest writing a view that uses the get_or_create model method.

Here is the documentation: https://docs.djangoproject.com/en/3.2/ref/models/querysets/#get-or-create

brandonris1
  • 455
  • 3
  • 9