标签: django-managers

Django:你如何从经理内部访问模型的实例?

class SupercalifragilisticexpialidociousManager(models.Manager):
    # Sorry, I'm sick of Foo and Spam for now.
    def get_query_set(self, account=None):
        return super(SupercalifragilisticexpialidociousManager,
                     self).get_query_set().filter(uncle=model_thats_using_this_manager_instance.uncle)
Run Code Online (Sandbox Code Playgroud)

我正在寻找的魔法是"叔叔= model_thats_using_this_manager_instance.uncle".看起来我应该能够以某种方式做到这一点.我知道我可以说self.model要获得模型,但如何获取实例?

django django-models django-managers

2
推荐指数
1
解决办法
5787
查看次数

django访问自定义管理器中登录的用户

我想在我写过的自定义管理器中访问当前登录的用户.我想这样做,以便我可以过滤结果只显示他们有权访问的对象.

有没有这样做而没有实际传递它?类似于它在您可以执行request.user的视图中的工作方式.

谢谢

django django-managers django-users

2
推荐指数
1
解决办法
1908
查看次数

如何将 request.user 传递给模型管理器?

我有一个模型管理器get_queryset

class BookManager(models.Manager):

    def get_queryset(self):

        return super().get_queryset().filter(author=self.request.user
Run Code Online (Sandbox Code Playgroud)

这导致错误:

AttributeError: 'BookManager' 对象没有属性 'request`

我知道经理是“不知道请求的”。我将如何在您的经理/查询集中创建一个为 request.user 提供参数的方法?

django django-models django-managers

1
推荐指数
1
解决办法
550
查看次数

django经理的问题

我有以下型号:

class UserProfile(models.Model):
    """
    User profile model, cintains a Foreign Key, which links it to the
    user profile.
    """
    about = models.TextField(blank=True)
    user = models.ForeignKey(User, unique=True)
    ranking = models.IntegerField(default = 1)
    avatar = models.ImageField(upload_to="usermedia", default = 'images/js.jpg')
    updated = models.DateTimeField(auto_now=True, default=datetime.now())
    is_bot = models.BooleanField(default = False)
    is_active = models.BooleanField(default = True)
    is_free = models.BooleanField(default = True)
    objects = ProfileManager()

    def __unicode__(self):
        return u"%s profile" %self.user
Run Code Online (Sandbox Code Playgroud)

还有经理

class ProfileManager(models.Manager):
    """
    Stores some additional helpers, which to get some profile data
    """ …
Run Code Online (Sandbox Code Playgroud)

django django-models django-managers

0
推荐指数
1
解决办法
1696
查看次数

默认模型管理器只获取属于登录用户的对象

当我得到一个对象列表时,我只想要属于登录用户的对象。

我在想,也许这可以通过覆盖默认管理器在模型管理器中完成,但我不确定如何登录使用。这是我到目前为止...

class GroupsManager(models.Manager):
    def get_query_set(self):
        return super(GroupsManager, self).get_query_set().filter(user=???????)
Run Code Online (Sandbox Code Playgroud)

我在正确的轨道上吗?我怎样才能实现我的目标?或者有没有更好的方法来做到这一点?

谢谢。

django django-models django-managers

0
推荐指数
1
解决办法
517
查看次数

如何调用模型方法?

我试图只显示不超过4天的对象.我知道我可以使用过滤器:

new = Books.objects.filter(pub_date__gt = datetime.now() - timedelta(days=4))
Run Code Online (Sandbox Code Playgroud)

但我真的想用一种模态方法进行锻炼.

该方法在模型Book中定义,称为published_recetnly.

所以我的问题是如何在views.py中调用模态方法?

这是我目前的代码:

views.py

def index(request):
    new = Books.objects.filter(pub_date__gt = datetime.now() - timedelta(days=4))
    return render_to_response('books/index.html', {'new':new}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

的index.html

 {% if book in new %}
    {{ book.title }}
 {% endif %}
Run Code Online (Sandbox Code Playgroud)

models.py

class Book(models.Model)
    pub_date = models.DateTimeField('date published')

    def published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=4) <= self.pub_date <= now
Run Code Online (Sandbox Code Playgroud)

python django django-models django-queryset django-managers

0
推荐指数
1
解决办法
619
查看次数