从中查询获取数据库的django模型对象

时间:2012-04-29 18:59:52

标签: django django-models

我有一个使用多个数据库的应用程序。一旦模型实例对象在内存中,我如何确定它来自哪个数据库?具体来说,我想知道这个用于模型类的方法。

示例:

class book(Models.model):
    newdate = models.DateField(default=date.today())
    type = models.CharField(
                            max_length=30,
                            choices=BOOK_TYPE,
                            default = 'novel',
                            )
    def get_current_student(self):
        if not hasattr(self,'_current_student'):
            try:
                self._current_student = clickerlog.objects.using(SELF.ORIGIN_DATABASE).get(book=self.pk,end__isnull = True).student
            except:
                self._current_student = none
        return self._current_student

class booklog(Models.model):
    start = models.DateTimeField(
                                 default=datetime.now(),
                                 verbose_name = 'start time'
                                 )
    end = models.DateTimeField(null=True,blank=True,)
    book = models.ForeignKey(book)
    student = models.ForeignKey(student,
                              limit_choices_to = {'isactive':True})

1 个答案:

答案 0 :(得分:19)

经过一番挖掘,我找到了它:

self._state.db