【问题标题】:TastyPie how to access objects Manager methods?TastyPie 如何访问对象管理器方法?
【发布时间】:2013-11-29 10:46:41
【问题描述】:

使用 TastyPie 的 obj_create 方法我想覆盖包保存并调用我的模型对象管理器方法create_member() 但尝试这样做时出现以下错误:

Manager isn't accessible via MemberParticipant instances

如何在 TastyPie 中访问我的对象管理器方法? bundle.obj 似乎没有访问权限!

试过了:

def obj_create(self, bundle, request=None, **kwargs):
    bundle.obj = self._meta.object_class()
    bundle.obj.objects.create_member(stuff)
 return bundle

型号:

class MemberManager(models.Manager):
    def create_member(self, email, password=None, **extra_fields):
        """
        Creates and saves new Member Participant with the given email, password and account.
        """
        time_now = timezone.now()
        stuff here


class MemberParticipant(AbstractParticipant):
    email = models.CharField(max_length=50, blank=True)
    objects = MemberManager()
    class Meta:
        app_label = 'participants'

【问题讨论】:

    标签: python django


    【解决方案1】:

    在 django 模型对象上,_default_manager 属性存储了默认的对象管理器实例。您可以尝试使用它。

    >>> m=Model1.objects.all()[0]
    >>> m._default_manager
    <django.db.models.manager.Manager object at 0x2a38b90>
    >>> 
    

    但是,我不确定 tasypie 的表现如何。

    【讨论】:

    • 不,似乎也不喜欢那样。为什么我无法访问此管理器? Tatypie 有什么特殊的刻度吗?从查看美味的代码 obj 只是 = 到类,所以 obj.objects 应该可以正常工作吗?
    • @GrantU,如果obj 是类/模型,那么obj.objects 应该可以工作。
    • 是的,它应该是它。
    • @GrantU,您在方法中覆盖了bundle.obj,这可能会导致问题。
    • @GrantU,同意,但是文档说bundle.obj 可能还没有保存,所以你仍然可以使用你的经理创建对象并跳过美味的。
    猜你喜欢
    • 1970-01-01
    • 2011-11-15
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2020-02-26
    • 2016-06-24
    相关资源
    最近更新 更多