blondelg
blondelg

Reputation: 986

What is the best way to setup default snippets in Wagtail

I would like to setup default snippets (article categories) in a Wagtail blog project.

My option would be to add the following line at the end of the models.py module:

if BlogPageCategory.objects.filter(name = 'Association').count() == 0:
    category = BlogPageCategory(name='Association')
    category.save()

Is it the best way to do it? Would it have any impact on the site performance?

Thanks

Upvotes: 2

Views: 230

Answers (1)

Nico Griffioen
Nico Griffioen

Reputation: 5405

There are multiple ways to achieve what you want:

  • The recommended way to do this is using Data migrations. The advantage of this method is that setting defaults in your database will be done when initializing, instead of a runtime action.
  • The way you described (Although it'd be best to do this in your AppConfig's ready() method (as described here), as that is most commonly the place to run any code upon server startup. It won't have a very big impact on performance, because it is only run once (When your server is started)
  • Using fixtures, which IMO is only necessary when loading large amounts of data into your database.

Upvotes: 3

Related Questions