0

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

1 Answer 1

1

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')
1
  • 1
    Thank you for your advice and explanation. Apr 11, 2016 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.