Python script calls Jdango orm Mapping method

Source: Internet
Author: User
The Django project is running under Web server. However, it is often necessary to develop an external script to deal with the model of this Web project. For example, you would call the Django model to write data through an external Python script, or query the data. The following script demonstrates how to query a defined alias field with Django modes
# _*_ coding: utf-8 _*_#!/usr/bin/env python__author__ = ‘xiaoke‘__date__ = ‘2018/4/27 14:38‘import sys,osimport django#定义django项目所在的路径【django版本1.9.8;python版本2.7】sys.path.append(r‘/opt/CxOps/‘)os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘CxOps.settings‘)django.setup()#测试settings文件是否可以import#from CxOps import settings#print(settings.ROOT_URLCONF)#import models,和在jdango里面写法一样了from ops_web.models import HostPrivilegesall_alias = HostPrivileges.objects.get(username="gukun")for alias in all_alias.alias.all():    #print(type(alias))    print(str(alias) + ‘------>‘ + alias.inip)    #print(alias.inip)

Python script calls Jdango orm Mapping method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.