本文整理汇总了Python中django.contrib.contenttypes.models.ContentType.DoesNotExist方法的典型用法代码示例。如果您正苦于以下问题:Python ContentType.DoesNotExist方法的具体用法?Python ContentType.DoesNotExist怎么用?Python ContentType.DoesNotExist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.contenttypes.models.ContentType
的用法示例。
在下文中一共展示了ContentType.DoesNotExist方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_relations
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def update_relations(self, source_instance, data):
relations = []
src_ct = ContentType.objects.get_for_model(source_instance)
for model, link in registry.model_links.items():
parser, url, reverse = link
tgt_ct = None
for match in parser.finditer(data):
if match:
if tgt_ct is None:
try:
tgt_ct = ContentType.objects.get_by_natural_key(*model.lower().split('.'))
except ContentType.DoesNotExist:
continue
relation, created = self.get_or_create(
src_content_type=src_ct,
src_object_id=source_instance.pk,
tgt_content_type=tgt_ct,
tgt_object_id=match.group(1)
)
relations.append(relation)
return relations
示例2: handle_notification
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def handle_notification(content_type, instance, business_lines, event):
from fir_notifications.registry import registry
from django.contrib.contenttypes.models import ContentType
try:
model = ContentType.objects.get_for_id(content_type).model_class()
except ContentType.DoesNotExist:
print("Unknown content type")
return
try:
instance = model.objects.get(id=instance)
except model.DoesNotExist:
print("Unknown instance")
return
users = get_user_templates(event, business_lines)
for method in registry.get_methods():
method.send(event, users, instance, business_lines)
示例3: autocomplete
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def autocomplete(request, app_name=None, model_name=None):
if app_name and model_name:
try:
content_type = ContentType.objects.get_by_natural_key(app_name, model_name)
except ContentType.DoesNotExist:
raise Http404
tag_model = content_type.model_class()
if not issubclass(tag_model, TagBase):
raise Http404
else:
tag_model = Tag
term = request.GET.get('term', None)
if term:
tags = tag_model.objects.filter(name__istartswith=term).order_by('name')
else:
tags = tag_model.objects.none()
return JsonResponse([tag.name for tag in tags], safe=False)
示例4: content_type_use
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def content_type_use(request, content_type_app_name, content_type_model_name):
try:
content_type = ContentType.objects.get_by_natural_key(content_type_app_name, content_type_model_name)
except ContentType.DoesNotExist:
raise Http404
page_class = content_type.model_class()
# page_class must be a Page type and not some other random model
if not issubclass(page_class, Page):
raise Http404
pages = page_class.objects.all()
paginator = Paginator(pages, per_page=10)
pages = paginator.get_page(request.GET.get('p'))
return TemplateResponse(request, 'wagtailadmin/pages/content_type_use.html', {
'pages': pages,
'app_name': content_type_app_name,
'content_type': content_type,
'page_class': page_class,
})
示例5: get_page
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def get_page(self):
(content_type_app_name, content_type_model_name,
parent_page_id) = self.args
try:
content_type = ContentType.objects.get_by_natural_key(
content_type_app_name, content_type_model_name)
except ContentType.DoesNotExist:
raise Http404
page = content_type.model_class()()
parent_page = get_object_or_404(Page, id=parent_page_id).specific
# We need to populate treebeard's path / depth fields in order to
# pass validation. We can't make these 100% consistent with the rest
# of the tree without making actual database changes (such as
# incrementing the parent's numchild field), but by calling treebeard's
# internal _get_path method, we can set a 'realistic' value that will
# hopefully enable tree traversal operations
# to at least partially work.
page.depth = parent_page.depth + 1
# Puts the page at the maximum possible path
# for a child of `parent_page`.
page.path = Page._get_children_path_interval(parent_page.path)[1]
return page
示例6: index
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def index(request, page_pk=None):
page_qs = HelpBasePage.objects.filter(tree_id=1)
# either of these should result in a QuerySet with one result
if page_pk is not None:
page_qs = page_qs.filter(pk=page_pk)
else:
page_qs = page_qs.filter(parent__isnull=True)
try:
page = page_qs.get()
except HelpBasePage.DoesNotExist:
raise Http404
context = {
"page": page,
"breadcrumbs": breadcrumb_iterator(page),
}
return TemplateResponse(
request,
"cms/admin/index.html",
context,
)
示例7: choose_page_type
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def choose_page_type(request, parent_pk):
try:
page = HelpBasePage.objects.get(pk=parent_pk)
except HelpBasePage.DoesNotExist:
raise Http404
context = {
"models": [model._meta for model in PAGE_TYPES],
"parent_pk": parent_pk,
"breadcrumbs": breadcrumb_iterator(page),
}
return TemplateResponse(
request,
"cms/admin/choose_page_type.html",
context,
)
示例8: forwards
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def forwards(self, orm):
pass
# remove old permission label if migrated with old model metadata
try:
ct = ContentType.objects.get(model='xform', app_label='odk_logger')
Permission.objects.get(content_type=ct, codename='can_view').delete()
# add new permission label
perm, created = Permission.objects.get_or_create(content_type=ct, codename='view_xform', name='Can view associated data')
except (ContentType.DoesNotExist, Permission.DoesNotExist):
pass
示例9: get_model_form
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def get_model_form(metadata_class):
model_class = metadata_class._meta.get_model('model')
# Restrict content type choices to the models set in seo_models
content_types = get_seo_content_types(metadata_class._meta.seo_models)
content_type_choices = [(x._get_pk_val(), smart_str(x)) for x in
ContentType.objects.filter(id__in=content_types)]
# Get a list of fields, with _content_type at the start
important_fields = ['_content_type'] + core_choice_fields(metadata_class)
_fields = important_fields + list(fields_for_model(model_class,
exclude=important_fields).keys())
class ModelMetadataForm(forms.ModelForm):
_content_type = forms.ChoiceField(label=capfirst(_("model")),
choices=content_type_choices)
class Meta:
model = model_class
fields = _fields
def clean__content_type(self):
value = self.cleaned_data['_content_type']
try:
return ContentType.objects.get(pk=int(value))
except (ContentType.DoesNotExist, ValueError):
raise forms.ValidationError("Invalid ContentType")
return ModelMetadataForm
示例10: _get_relobjs
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def _get_relobjs(obj,field,filters=None):
"""Find related objects that match
Could be linked using a "related_name" or as <type>_set
django versions have changed this around so somewhat tricky..
"""
# then try to find objects of this type with a foreign key property using either (name) supplied or target object type
if not filters:
filters = { 'includes': {} , 'excludes' : {} }
if field.endswith(")") :
(field, relprop ) = str(field[0:-1]).split("(")
else :
relprop = None
try:
reltype = ContentType.objects.get(model=field)
except ContentType.DoesNotExist as e :
raise ValueError("Could not locate attribute or related model '{}' in element '{}'".format(field, type(obj)) )
# if no related_name set in related model then only one candidate and djanog creates X_set attribute we can use
try:
return getattr(obj, "".join((field,"_set"))).filter(**filters['includes']).exclude(**filters['excludes'])
except:
pass
# trickier then - need to look at models of the named type
claz = reltype.model_class()
for prop,val in list(claz.__dict__.items()) :
# skip related property names if set
if relprop and prop != relprop :
continue
if relprop or type(val) is ReverseSingleRelatedObjectDescriptor and val.field.related.model == type(obj) :
filters['includes'].update({prop:obj})
return claz.objects.filter(**filters['includes']).exclude(**filters['excludes'])
示例11: content_type
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def content_type(self):
try:
return ContentType.objects.get_for_id(self.content_type_id)
except ContentType.DoesNotExist:
raise Http404
示例12: build_permission_groups
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def build_permission_groups(app_label):
# get just the base models, we'll use this to build the
# base groups
DynamicModel = ContentType.objects.get(
app_label=app_label, model="dynamicmodel"
).model_class()
Permission, Group = get_permission_models()
dynmodels = DynamicModel.objects.exclude(name__endswith="metadata")
for dynmodel in dynmodels:
name = dynmodel.name
meta_name = "%smetadata" % (name)
contact_meta_name = "%scontactmetadata" % (name)
try:
content_type = ContentType.objects.get(
app_label=app_label,
model=name,
)
content_type_meta = ContentType.objects.get(
app_label=app_label,
model=meta_name,
)
content_type_contactmeta = ContentType.objects.get(
app_label=app_label,
model=contact_meta_name,
)
except ContentType.DoesNotExist:
return
# create the permission group for this source. it simply bears the
# name of the source and grants all perms
perm_group, created = Group.objects.get_or_create(
name=dynmodel.name
)
perms = list(Permission.objects.filter(content_type__in=[
content_type, content_type_meta, content_type_contactmeta
]))
perm_group.permissions.add(*perms)
perm_group.save()
示例13: get_model_form
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def get_model_form(metadata_class):
model_class = metadata_class._meta.get_model('model')
# Restrict content type choices to the models set in seo_models
content_types = get_seo_content_types(metadata_class._meta.seo_models)
content_type_choices = [(x._get_pk_val(), smart_text(x)) for x in
ContentType.objects.filter(id__in=content_types)]
# Get a list of fields, with _content_type at the start
important_fields = ['_content_type'] + core_choice_fields(metadata_class)
_fields = important_fields + list(fields_for_model(model_class,
exclude=important_fields).keys())
class ModelMetadataForm(forms.ModelForm):
_content_type = forms.ChoiceField(label=capfirst(_("model")),
choices=content_type_choices)
class Meta:
model = model_class
fields = _fields
def clean__content_type(self):
value = self.cleaned_data['_content_type']
try:
return ContentType.objects.get(pk=int(value))
except (ContentType.DoesNotExist, ValueError):
raise forms.ValidationError("Invalid ContentType")
return ModelMetadataForm
示例14: get_model_class
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def get_model_class(self):
"""Returns model class"""
try:
c = ContentType.objects.get(app_label=self.app, model=self.model)
except ContentType.DoesNotExist:
# try another kind of resolution
# fixes a situation where a proxy model is defined in some external app.
if django.VERSION >= (1, 7):
return apps.get_model(self.app, self.model)
else:
return c.model_class()
示例15: create_page
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import DoesNotExist [as 别名]
def create_page(request, model, parent_pk):
try:
page = HelpBasePage.objects.get(pk=parent_pk)
except HelpBasePage.DoesNotExist:
raise Http404
try:
model_ct = ContentType.objects.get_by_natural_key(app_label="cms", model=model)
except ContentType.DoesNotExist:
raise Http404
form_class = get_page_form(model_ct)
if request.method == "POST":
form = form_class(data=request.POST, files=request.FILES)
form.instance.parent_id = parent_pk
form.instance.content_type = model_ct
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse("admin:index", kwargs={"page_pk": parent_pk}))
else:
form = form_class()
return TemplateResponse(
request,
"cms/admin/create_page.html",
{
"form": form,
"breadcrumbs": breadcrumb_iterator(page),
}
)