def doquery(request, cls, related=False): fields = request.POST.get("fields", []) conditions = request.POST.get("conditions", []) orderby = request.POST.get("orderby", []) limit = request.POST.get("limit", None) if fields: fields = json.loads(fields) if fields and "id" not in fields: fields.append("id") if not fields: fields = get_fields_names(cls) if conditions: conditions = json.loads(conditions) if orderby: orderby = json.loads(orderby) if limit: limit = json.loads(limit) if related: relation = request.POST.get("related", "{}") relation = json.loads(relation) relation = relation if relation else {} if not relation: related = False kwargs = {} for item in conditions: key = "__".join(item[0:2]) kwargs[key] = item[2] if related: obj = qs_replace(cls.objects.filter(**kwargs).order_by(*orderby), relation, fields) else: obj = cls.objects.filter(**kwargs).order_by(*orderby).values(*fields) return obj[limit[0]:limit[1]] if limit else obj
def test(request): ret = qs_replace(Publication.objects.filter(id__exact=1), {"category": "name"}) return HttpResponse(json.dumps(ret, cls=DjangoJSONEncoder))