本文整理汇总了Python中wagtail.wagtailcore.utils.resolve_model_string函数的典型用法代码示例。如果您正苦于以下问题:Python resolve_model_string函数的具体用法?Python resolve_model_string怎么用?Python resolve_model_string使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了resolve_model_string函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: page_model_from_string
def page_model_from_string(string):
page_model = resolve_model_string(string)
if not issubclass(page_model, Page):
raise ValueError("Model is not a page")
return page_model
示例2: target_content_type
def target_content_type(cls):
if cls._target_content_type is None:
if cls.page_type:
target_models = []
for page_type in cls.page_type:
try:
target_models.append(resolve_model_string(page_type))
except LookupError:
raise ImproperlyConfigured(
"{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
cls.__name__, page_type
)
)
except ValueError:
raise ImproperlyConfigured(
"{0}.page_type refers to model {1!r} that has not been installed".format(
cls.__name__, page_type
)
)
cls._target_content_type = list(ContentType.objects.get_for_models(*target_models).values())
else:
target_model = cls.model._meta.get_field(cls.field_name).rel.to
cls._target_content_type = [ContentType.objects.get_for_model(target_model)]
return cls._target_content_type
示例3: get_invoice_model
def get_invoice_model(cls):
if isinstance(cls.invoice_model, models.Model):
return cls.invoice_model
elif isinstance(cls.invoice_model, string_types):
return resolve_model_string(cls.invoice_model, cls._meta.app_label)
else:
raise ValueError('Can not resolve {0}.invoice_model in to a model: {1!r}'.format(
cls.__name__, cls.invoice_model))
示例4: get_model
def get_model(self, request):
if 'type' not in request.GET:
return Page
model_name = request.GET['type']
try:
return resolve_model_string(model_name)
except LookupError:
raise self.BadRequestError("type doesn't exist")
示例5: target_models
def target_models(self):
target_models = []
for target_model in self._target_models:
target_models.append(
resolve_model_string(target_model)
)
return target_models
示例6: target_model
def target_model(self):
"""
Defines the model used by the base ChooserBlock for ID <-> instance
conversions. If a single page type is specified in target_model,
we can use that to get the more specific instance "for free"; otherwise
use the generic Page model.
"""
if len(self.target_models) == 1:
return self.target_models[0]
return resolve_model_string('wagtailcore.Page')
示例7: page_models_from_string
def page_models_from_string(string):
page_models = []
for sub_string in string.split(','):
page_model = resolve_model_string(sub_string)
if not issubclass(page_model, Page):
raise ValueError("Model is not a page")
page_models.append(page_model)
return tuple(page_models)
示例8: get_model
def get_model(self, request):
if 'type' not in request.GET:
return Page
model_name = request.GET['type']
try:
model = resolve_model_string(model_name)
if not issubclass(model, Page):
raise BadRequestError("type doesn't exist")
return model
except LookupError:
raise BadRequestError("type doesn't exist")
示例9: target_model
def target_model(cls):
if cls._target_model is None:
if cls.snippet_type:
try:
cls._target_model = resolve_model_string(cls.snippet_type)
except LookupError:
raise ImproperlyConfigured("{0}.snippet_type must be of the form 'app_label.model_name', given {1!r}".format(
cls.__name__, cls.snippet_type))
except ValueError:
raise ImproperlyConfigured("{0}.snippet_type refers to model {1!r} that has not been installed".format(
cls.__name__, cls.snippet_type))
else:
cls._target_model = cls.model._meta.get_field(cls.field_name).rel.to
return cls._target_model
示例10: target_content_type
def target_content_type(cls):
if cls._target_content_type is None:
if cls.page_type:
try:
model = resolve_model_string(cls.page_type)
except LookupError:
raise ImproperlyConfigured("{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
cls.__name__, cls.page_type))
except ValueError:
raise ImproperlyConfigured("{0}.page_type refers to model {1!r} that has not been installed".format(
cls.__name__, cls.page_type))
cls._target_content_type = ContentType.objects.get_for_model(model)
else:
# TODO: infer the content type by introspection on the foreign key
cls._target_content_type = ContentType.objects.get_by_natural_key('wagtailcore', 'page')
return cls._target_content_type
示例11: search
def search(request, parent_page_id=None):
page_types = request.GET.get('page_types')
content_types = []
# Convert page_types string into list of ContentType objects
if page_types:
try:
content_types = ContentType.objects.get_for_models(*[
resolve_model_string(page_type) for page_type in page_types.split(',')])
except LookupError:
raise Http404
search_form = SearchForm(request.GET)
if search_form.is_valid() and search_form.cleaned_data['q']:
pages = Page.objects.exclude(
depth=1 # never include root
)
# Restrict content types
if content_types:
pages = pages.filter(content_type__in=content_types)
# Do search
pages = pages.filter(title__icontains=search_form.cleaned_data['q'])
# Truncate results
pages = pages[:10]
else:
pages = Page.objects.none()
shown_pages = []
for page in pages:
page.can_choose = True
shown_pages.append(page)
return render(
request, 'wagtailadmin/chooser/_search_results.html',
shared_context(request, {
'searchform': search_form,
'pages': shown_pages,
})
)
示例12: target_model
def target_model(cls):
if cls._target_model is None:
if cls.snippet_type:
# RemovedInWagtail16Warning: The target_model is automatically
# detected from the relation, so snippet_type is deprecated.
try:
cls._target_model = resolve_model_string(cls.snippet_type)
except LookupError:
raise ImproperlyConfigured(
"{0}.snippet_type must be of the form 'app_label.model_name', given {1!r}"
.format(cls.__name__, cls.snippet_type)
)
except ValueError:
raise ImproperlyConfigured(
"{0}.snippet_type refers to model {1!r} that has not been installed"
.format(cls.__name__, cls.snippet_type)
)
else:
cls._target_model = cls.model._meta.get_field(cls.field_name).rel.to
return cls._target_model
示例13: clean_parent_page_types
def clean_parent_page_types(cls):
"""
Returns the list of parent page types, with strings converted to class
objects where required
"""
if cls._clean_parent_page_types is None:
parent_page_types = getattr(cls, 'parent_page_types', None)
if parent_page_types is None:
# if parent_page_types is not specified on the Page class, allow all page types as subpages
res = get_page_types()
else:
try:
models = [resolve_model_string(model_string, cls._meta.app_label)
for model_string in parent_page_types]
except LookupError as err:
raise ImproperlyConfigured("{0}.parent_page_types must be a list of 'app_label.model_name' strings, given {1!r}".format(
cls.__name__, err.args[1]))
res = list(map(ContentType.objects.get_for_model, models))
cls._clean_parent_page_types = res
return cls._clean_parent_page_types
示例14: get_queryset
def get_queryset(self):
request = self.request
# Allow pages to be filtered to a specific type
if 'type' not in request.GET:
model = Page
else:
model_name = request.GET['type']
try:
model = resolve_model_string(model_name)
except LookupError:
raise BadRequestError("type doesn't exist")
if not issubclass(model, Page):
raise BadRequestError("type doesn't exist")
# Get live pages that are not in a private section
queryset = model.objects.public().live()
# Filter by site
queryset = queryset.descendant_of(request.site.root_page, inclusive=True)
return queryset
示例15: target_models
def target_models(cls):
if cls.page_type:
target_models = []
for page_type in cls.page_type:
try:
target_models.append(resolve_model_string(page_type))
except LookupError:
raise ImproperlyConfigured(
"{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
cls.__name__, page_type
)
)
except ValueError:
raise ImproperlyConfigured(
"{0}.page_type refers to model {1!r} that has not been installed".format(
cls.__name__, page_type
)
)
return target_models
else:
return [cls.model._meta.get_field(cls.field_name).rel.to]