本文整理汇总了Python中django.contrib.contenttypes.models.ContentType.model_class方法的典型用法代码示例。如果您正苦于以下问题:Python ContentType.model_class方法的具体用法?Python ContentType.model_class怎么用?Python ContentType.model_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.contenttypes.models.ContentType
的用法示例。
在下文中一共展示了ContentType.model_class方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: json_list
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def json_list(request,app,model,lookup,value):
content = ContentType(app_label=app,model=model)
contentclass = content.model_class()
lookup_dict = { lookup:value }
queryset = contentclass.objects.filter(**lookup_dict)
return HttpResponse(serializers.serialize('json', queryset),
mimetype='application/json')
示例2: get_validation_errors
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def get_validation_errors(self, token):
model_class = ContentType.model_class(self.lookup_type)
vals = model_class.objects.values_list(self.field_name, flat=True)
print "validating %s with %s" % (token, str(self))
if token not in vals:
return "%s not in list of %s %s" % (token, self.lookup_type.name, self.field_name)
return None
示例3: get_typeahead
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def get_typeahead(request):
if request.is_ajax() and request.method == "POST":
if ('field' in request.POST and
'app' in request.POST and
'model' in request.POST):
ct = ContentType.objects.get_by_natural_key(request.POST['app'],
request.POST['model'])
instance = ContentType.model_class(ct)
f = dict([(x,x) for x in instance._meta.get_all_field_names() ])
try:
o = f[request.POST['field']]
o = instance._meta.get_field_by_name(o)[0]
except KeyError:
return HttpResponseForbidden('[{"error":"Forbidden"}]',
mimetype='application/json; charset=utf8')
o = o.related.parent_model
obj_list = o.objects.all()
lst = {}
for i,obj in enumerate(obj_list):
l = {}
l.update({"id":obj.id})
l.update({"unicode":obj.__unicode__()})
#not sure about __unicode__, actually
lst.update({i:l})
return HttpResponse(json.dumps(lst, indent = 4 * ' '),
mimetype='application/json; charset=utf8')
else:
return HttpResponseForbidden('[{"error":"Forbidden. Wrong headers."}]',
mimetype='application/json; charset=utf8')
示例4: get_validation_errors
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def get_validation_errors(self, token_value):
model_class = ContentType.model_class(self.lookup_type)
lookup = { str(self.field_name + "__iexact") : token_value.data }
try:
val = model_class.objects.get(**lookup)
return None
except model_class.DoesNotExist:
return "%s not in list of %s %ss" % (token_value.data, self.lookup_type.name, self.field_name)
示例5: get_absolute_url
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def get_absolute_url(self):
"""
link to dorsale’s generic `show_item` view
"""
mo = ContentType.model_class(self)
return ('dorsale.views.show_item', (), {
'app_name' : mo.app_label,
'model_name': mo.model,
'object_id' : self.id,
})
示例6: use_filter_internal
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def use_filter_internal(filter_id):
if filter_id:
try:
flt = Filter.objects.only('content_type').get(pk = filter_id)
except Filter.DoesNotExist:
return None
model = ContentType.model_class(flt.content_type)
kwargs = {}
for c in flt.conditions.all():
field = None
lookup = c.operator
field = "%s%s" % (c.field, lookup)
kwargs.update({field:c.value})
return model.objects.filter(**kwargs)
else:
return None
示例7: use_filter
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def use_filter(request):
if request.is_ajax():
if 'filter_id' in request.GET:
try:
flt = Filter.objects.only('content_type').get(pk = request.GET['filter_id'])
except Filter.DoesNotExist:
return HttpResponseForbidden('[{"error":"Filter Not found."}]',
mimetype='application/json; charset=utf8')
model = ContentType.model_class(flt.content_type)
kwargs = {}
for c in flt.conditions.all():
field = None
lookup = c.operator
field = "%s%s" % (c.field, lookup)
kwargs.update({field:c.value})
qs = model.objects.filter(**kwargs)
response = {}
for i,q in enumerate(qs):
field_list = {}
for f in q._meta.get_all_field_names():
obj, model, direct, m2m = q._meta.get_field_by_name(f)
if not direct or isinstance(obj, GenericRelation):
continue
if m2m:
l = {}
val = obj.value_from_object(q)
for m in obj.value_from_object(q):
l.update({m.pk:m.__unicode__()})
field_list.update({f:l})
elif obj.rel:
val = q.__getattribute__(obj.name)
if val:
l = {val.pk:val.__unicode__()}
field_list.update({obj.name:l})
else:
field_list.update({f:None})
else:
field_list.update({f:obj.value_to_string(q)})
response.update({i:field_list})
r = json.dumps(response, indent = 4 * ' ')
return HttpResponse(r,
mimetype='application/json; charset=utf8')
return HttpResponseForbidden('[{"error":"Forbidden. Wrong headers."}]',
mimetype='application/json; charset=utf8')
示例8: get_structure
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import model_class [as 别名]
def get_structure(request):
if request.method == "POST" and request.is_ajax():
if 'app' in request.POST and 'model' in request.POST:
fields = {}
ct = ContentType.objects.get_by_natural_key(request.POST['app'],
request.POST['model'])
model = ContentType.model_class(ct)
for i,x in enumerate(model._meta.get_all_field_names()):
obj, m, direct, m2m = model._meta.get_field_by_name(x)
if obj.name == 'id' or not direct or isinstance(obj, GenericRelation):
continue
f = {}
f.update({"type":obj.get_internal_type()})
f.update({"name":obj.name})
fields.update( {i: f} )
r = {}
r.update({'fields':fields})
r.update({'operators':LOGICAL_OPERATORS})
return HttpResponse(json.dumps(r, indent = 4 * ' '),
mimetype='application/json; charset=utf8')
return HttpResponseForbidden('[{"error":"Forbidden"}]',
mimetype='application/json; charset=utf8')