1

I store some temporary data in a database linked to a web socket. After a server reboot, the web sockets die and I want to delete all data from the corresponding table. Tell me where ideologically this should be done or such data should not be stored in the database at all?

I tried putting this block in AppConfig.ready but the console warning says it's the wrong place.

from django.apps import AppConfig

class GameConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'game'

    def ready(self) -> None:
        LobbySetting = self.get_model('LobbySetting')
        LobbySetting.objects.all().delete()

Warning in console:

RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.
  warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
3
  • I did not testet it, but may be post_migrate signal could be a viable option: docs.djangoproject.com/en/5.1/ref/signals/#post-migrate Commented Oct 17, 2024 at 6:05
  • When you think of storing data, there is no better solution than a database, its that simple. Although, there are many options that would make one achieve a better result, in this case a NoSQL database (REDIS, Mongo) would fit better, imo. Commented Nov 1, 2024 at 11:10
  • As for the clearing data issue, there are other ways. I do not know exactly how your app works, so its hard to say. But for instance, how about a job scheduler to clear all data that’s one week old (or similar). Commented Nov 1, 2024 at 11:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.