django 和 appengine 模型类之间的代码重用
我创建了一个自定义的 django.auth User 类,它与 Google Appengine 一起使用,但它涉及大量复制代码(几乎每个方法)。
不可能创建子类,因为 appengine 和 django 有不同的数据库模型,有自己的元类魔法。
所以我的问题是:有没有一种优雅的方法从 django.auth 的 User 类复制方法?
from google.appengine.ext import db
from django.contrib.auth import models
class User(db.Model):
password = db.StringProperty()
...
# copied method
set_password = models.User.set_password.im_func
I created a custom django.auth User class which works with Google Appengine, but it involves a fair amount of copied code (practically every method).
It isn't possible to create a subclass because appengine and django have different database models with their own metaclass magic.
So my question is this: is there an elegant way to copy methods from django.auth's User class?
from google.appengine.ext import db
from django.contrib.auth import models
class User(db.Model):
password = db.StringProperty()
...
# copied method
set_password = models.User.set_password.im_func
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想看看 django 助手或 app-engine-patch 的作用。
帮助程序:http://code.google.com/p/google-app -engine-django/
补丁:http://code.google.com/p/app-engine-patch /
You might want to take a look at what the django helper or app-engine-patch does.
Helper: http://code.google.com/p/google-app-engine-django/
Patch: http://code.google.com/p/app-engine-patch/
我不确定我是否正确理解你的问题。 为什么你需要定义
如果 Django 已经提供了相同的功能,那么另一个“User”类?
您也可以只导入“User”类并向每个模型添加一个外键
需要“用户”属性。
Im not sure I understand your question right. Why would you need to define
another "User" class if Django already provides the same functionality ?
You could also just import the "User" class and add a ForeignKey to each model
requiring a "user" attribute.