|
3 | 3 | from django.contrib.auth.models import Permission |
4 | 4 | from django.contrib.contenttypes.models import ContentType |
5 | 5 | from django.contrib.sites.models import Site |
6 | | -from django.core.exceptions import ValidationError |
| 6 | +from django.core.exceptions import ValidationError, ObjectDoesNotExist |
7 | 7 | from django.db.models.fields import BooleanField |
8 | 8 | from django.forms.util import ErrorList |
9 | 9 | from django.forms.widgets import HiddenInput |
@@ -272,9 +272,23 @@ def __init__(self, *args, **kwargs): |
272 | 272 | if self.data.get('application_urls', False) and self.data['application_urls'] in app_configs: |
273 | 273 | self.fields['application_configs'].choices = [(config.pk, force_text(config)) for config in app_configs[self.data['application_urls']].get_configs()] |
274 | 274 |
|
275 | | - apphook = self.initial['application_urls'] |
276 | | - config = apphook_pool.get_apphook(apphook).get_configs().get(namespace=self.initial['application_namespace']) |
277 | | - self.fields['application_configs'].initial = config.pk |
| 275 | + apphook = self.data.get('application_urls', False) |
| 276 | + try: |
| 277 | + config = apphook_pool.get_apphook(apphook).get_configs().get(namespace=self.initial['application_namespace']) |
| 278 | + self.fields['application_configs'].initial = config.pk |
| 279 | + except ObjectDoesNotExist: |
| 280 | + # Provided apphook configuration doesn't exist (anymore), |
| 281 | + # just skip it |
| 282 | + # The user will choose another value anyway |
| 283 | + pass |
| 284 | + else: |
| 285 | + # If app_config apphook is not selected, drop any value |
| 286 | + # for application_configs do avoid the field dato for |
| 287 | + # being validated by the field itself |
| 288 | + try: |
| 289 | + del self.data['application_configs'] |
| 290 | + except KeyError: |
| 291 | + pass |
278 | 292 |
|
279 | 293 | if 'redirect' in self.fields: |
280 | 294 | self.fields['redirect'].widget.language = self.fields['language'].initial |
|
0 commit comments