django 和 appengine 模型类之间的代码重用

发布于 2024-07-24 10:56:16 字数 436 浏览 4 评论 0原文

我创建了一个自定义的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-07-31 10:56:16

您可能想看看 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/

夜无邪 2024-07-31 10:56:16

我不确定我是否正确理解你的问题。 为什么你需要定义
如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文