7
से पूछताछ की गई है मेरे पास एक ऐसा एप्लिकेशन है जो एकाधिक डेटाबेस का उपयोग करता है। एक बार मॉडल इंस्टेंस ऑब्जेक्ट मेमोरी में हो जाने पर, मैं यह निर्धारित कैसे कर सकता हूं कि यह किस डेटाबेस से आया था? विशेष रूप से मैं मॉडल वर्ग पर किसी विधि में उपयोग के लिए इसे जानना चाहता हूं।डेटाबेस प्राप्त करें django मॉडल ऑब्जेक्ट को
उदाहरण:
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})
जांच http://stackoverflow.com/questions/9929629/how-to-get-the-database -where-a-model-instance-was-saved-to-in-django – okm
संभावित डुप्लिकेट [क्या एक डीजेंगो मॉडल जानता है कि यह किस डेटाबेस से लोड किया गया था और यह जानकारी कैसे पहुंचा जा सकता है?] (http: // stackoverflow। com/प्रश्न/4146781/करता है एक Django मॉडल-पता-से-जो-डेटाबेस यह था-लोडेड और कैसे-कर सकते हैं-यह-जानकारी) – Louis