Andreas Schosser
Andreas Schosser

Reputation: 153

Disable validation when calling manage.py

Is it possible, to skip the validation when calling manage.py?

During development i have to reset my database from time to time an initialize data with "manage.py loaddata ...". In one of my template views i have following code:

class CharterModalEmailOption(MessagesMixin, CharterMixin, TemplateView):
    footer_template = Mailtemplate.objects.get(slug='signatur')

manage.py fails with the message "Mailtemplate matching query does not exist." That's right, because the table is empty at this stage and i want to add entries with loaddata. So as long as manage.py validates my views i cannot add the data. Looks like a dead end.

I can solve the problem by defining "footer_template" inside class functions and not at class level.

Does anybody have an idea?

Thank you, Andreas

Upvotes: 0

Views: 68

Answers (1)

Sayse
Sayse

Reputation: 43320

You shouldn't ever set a field directly to a queryset, and instead you should use a constructor.

Doing it directly will mean this is only ever done when the class is first referenced which may result in outdated information.

Simply just provide a constructor so this field is only initialized when you create an instance of the class

class CharterModalEmailOption(MessagesMixin, CharterMixin, TemplateView):
    def __init__(self, *args, **kwargs):
        self.footer_template = Mailtemplate.objects.get(slug='signatur')

Upvotes: 1

Related Questions