Fork me on GitHub Fork me on Gitee

python、django和pyamf

最近是学了有关用pyamf来连接python和django的东东,所以把自己的一些学习的步骤与体会记下来。环境:python-2.7 django-1.5 pyamf-0.6

第一部分:先建立一个admin,这个就按照<<djangobook2.0>>的步骤来做好了.

建好的工程和app

 

新建一个数据库onetest我用的是postgresql数据库

把数据库设置一下在setting

 

然后也是在setting中添加app

在url.py

 

models中

 1 # encoding=utf-8
 2 from django.db import models
 3 
 4 
 5 #定义出版商
 6 class Publisher(models.Model):
 7     #主键,id自增
 8     id=models.AutoField(primary_key=True) 
 9     #名字
10     publisher_name = models.CharField( max_length=30 ,verbose_name = "名字")
11     #地址
12     publisher_address = models.CharField( max_length=50,verbose_name = "地址")
13     #email
14     psublisher_email = models.EmailField(blank=True)
15     #禁用
16     enable = models.IntegerField(default=0,blank=True,verbose_name = "禁用")
17     
18     def __unicode__(self):
19         return self.publisher_name
20     
21     class Meta:
22         verbose_name = "出版商"
23         verbose_name_plural = "出版商"
24         
25         
26 #定义作者
27 class Author(models.Model):
28     #主键,id自增
29     id=models.AutoField(primary_key=True)
30     #名字
31     author_name = models.CharField(max_length=30 ,verbose_name = "名字")
32     #地址
33     author_address = models.CharField(max_length=50,verbose_name = "地址" )
34     #email
35     author_email = models.EmailField(blank=True)
36     #禁用
37     enable = models.IntegerField(default=0,blank=True,verbose_name = "禁用")
38     
39     def __unicode__(self):
40         return self.author_name       
41     
42     class Meta():
43         verbose_name = "作者"
44         verbose_name_plural = "作者"    
45      
46         
47 #定义书本
48 class Book(models.Model):
49     #主键,id自增 
50     id=models.AutoField(primary_key=True)
51     #名字
52     book_name = models.CharField( max_length=30 ,verbose_name = "书名")
53     #书作者
54     books_author = models.ManyToManyField(Author, related_name="book_author_r")
55     #书出版商
56     book_publisher = models.ForeignKey(Publisher,related_name="book_publisher_r")
57     #禁用
58     enable = models.IntegerField(default=0,blank=True,verbose_name = "禁用")
59     
60     def __unicode__(self):
61         return  self.book_name
62     
63     class Meta():
64         verbose_name = "书本"
65         verbose_name_plural = "书本" 

在testapp中新建一个admin.py文件

1 # encoding=utf-8
2 from django.contrib import admin
3 from models import *
4 
5 admin.site.register(Book)
6 admin.site.register(Author)
7 admin.site.register(Publisher)

 

在命令行输入manage.py syncdb
浏览器:(中文显示可以在setting里的LANGUAGE_CODE=‘en-us’换为'zh-CN')

 

 

 
 

 在testapp下建立gateway.py和publisher_action.py

publisher_action.py:

 1 #encoding=utf-8
 2 from django.db import transaction
 3 from django.conf import settings
 4 import models
 5 
 6 import logging
 7 
 8 
 9 
10 infoLog = logging.getLogger("info")
11 errorLog = logging.getLogger("error")
12 debugLog = logging.getLogger("debug")
13 
14 '''
15 接口
16     "publisher_name"=publisher_name,
17     "publisher_address"=publisher_address,
18     "publisher_email"=publisher_email,
19     "enable"=enable,
20 '''
21 
22 #新增publisher
23 def insert(data = {}): 
24     try:
25         model = models.Publisher(publisher_name=data.get("publisher_name"),  publisher_address=data.get("publisher_address"),  publisher_email=data.get("publisher_email"),enable=data.get("enable"))
26         if model.enable == None:
27             model.enable=0
28         model.save()
29         return model.id
30     except Exception,e:
31         errorLog.error(e.message)
32         return e
33     
34 #修改Publisher
35 def update(data = {}):
36     try:
37         try:
38             model = models.Publisher.objects.get(id=data.get("id"))
39         except Exception,ge:
40             errorLog.error(ge.message)
41             return 0
42         model.publisher_name=data.get("publisher_name")
43         model.publisher_address=data.get("publisher_address")
44         model.publisher_email=data.get("publisher_email")
45         model.enable=data.get("enable")
46         if model.enable == None:
47             model.enable=0
48         model.save()
49         return model.id
50     except Exception,e:
51         errorLog.error(e.message)
52         return None
53     
54 #删除Publisher
55 @transaction.commit_manually
56 def delete(datas = []):
57     try:
58         transaction.savepoint()
59         try:
60             for data in datas:
61                 model = models.Publisher.objects.get(id=data.get("id"))
62                 model.delete()
63             transaction.commit()
64             return len(datas)
65         except Exception,de:
66             transaction.rollback()
67             errorLog.error(de.message)
68             return 0
69         return True
70     except Exception,e:
71         errorLog.error(e.message)
72         return None

 

 gateway:

 1 #encoding=utf-8
 2 from pyamf.remoting.gateway.django import DjangoGateway
 3 import publisher_action
 4 import book_action
 5 
 6 
 7 services = {
 8     "publisher_action.insert":publisher_action.insert,
 9     "publisher_action.update":publisher_action.update,
10     "publisher_action.delete":publisher_action.delete,
11    
12 }
13 
14 recGW = DjangoGateway(services, expose_request=False, debug=True)
15 recGW.timezone_offset = 8 * 60 * 60 

 

 测试testing:

 1 # encoding=utf-8
 2 import logging
 3 
 4 logging.basicConfig(
 5     level=logging.DEBUG,
 6     format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
 7 )
 8 
 9 from pyamf.remoting.client import RemotingService
10 
11 url = 'http://127.0.0.1:8000/gateway/'
12 gw = RemotingService(url, logger=logging)
13 service = gw.getService('publisher_action')
14 
15 def insert():
16     print service.insert({"publisher_name":"xx","publisher_address":"xx","publisher_email":"123@123.com","enable":"0"})
17     
18 insert()

运行testing就可以在admin中的publisher看到新增的字段。

 在testapp中新建一个book_action:

 1 # encoding=utf-8
 2 from django.db import transaction
 3 from django.conf import settings
 4 import models
 5 from models import Author
 6 from models import Book
 7 from models import Publisher
 8 
 9 import logging
10 
11 infoLog = logging.getLogger("info")
12 errorLog = logging.getLogger("error")
13 debugLog = logging.getLogger("debug")
14 
15 '''
16 接口
17 "book_name"=book_name,
18 "books_author"=books_author,
19 "book_publisher"=book_publisher,
20 "enable"=enable,
21 '''
22 
23 #新增publisher
24 def insert(data = {}): 
25     try:
26         model = models.Book( book_name=data.get("book_name"),book_publisher_id=data.get("book_publisher_id"),enable=data.get("enable"))
27         if model.enable == None:
28             model.enable=0            
29         model.save()
30         
31         a = Author.objects.get(id=data.get("books_author_id_1"))            
32         c = Author.objects.get(id=data.get("books_author_id_2"))               
33         b = Book.objects.get(id = model.id)                                
34         b.books_author.add(a)     
35         b.books_author.add(c)          
36 
37         return model.id
38     except Exception,e:
39         errorLog.error(e.message)
40         return e

 

 testing修改为:

 1 # encoding=utf-8
 2 import logging
 3 
 4 logging.basicConfig(
 5     level=logging.DEBUG,
 6     format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
 7 )
 8 
 9 from pyamf.remoting.client import RemotingService
10 
11 url = 'http://127.0.0.1:8000/gateway/'
12 gw = RemotingService(url, logger=logging)
13 service = gw.getService('book_action')
14 
15 def insert():
16     print service.insert({"book_name":"xx","book_publisher_id":"1","books_author_id_1":"3","books_author_id_2":"4","enable":"0"})
17     
18 insert()

运行testing,这样就可以看到新增的book字段与author有多对多的关系。可能页面会出现bug,重开浏览器就不会了。

posted @ 2013-05-14 20:45  伴途の永远  阅读(782)  评论(0编辑  收藏  举报