I am new to Django 2.2 and I am building a small project that includes 2 databases not managed by Django. One is new and the other is legacy. I added them to the database list
DATABASES = {
'default': {
'ENGINE' :'django.db.backends.mysql',
'NAME': 'petshows',
'USER':'somecoolusername',
'PASSWORD': 'somesecurepassword',
'HOST': 'localhost',
'PORT':'3306'
},
'pets': {
'ENGINE' :'django.db.backends.mysql',
'NAME': 'pets',
'USER':'mysecureusers',
'PASSWORD': 'somecoolpassword',
'HOST': 'localhost',
'PORT':'3306'
}
}
I created models for petshows which seem to work fine. But I need to add some models so I can read foreign keys from the second database 'pets'.
My models.py
class ShowPets(models.Model):
sp_id = models.IntegerField(primary_key=True)
pet_id = models.IntegerField(blank=True, null=True)
show_id = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'show_pets'
class Shows(models.Model):
show_id = models.AutoField(primary_key=True)
show_name = models.CharField(max_length=145, blank=True, null=True)
show_date = models.DateField(blank=True, null=True)
location_id = models.IntegerField(blank=True, null=True)
org_id = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField(blank=True, null=True)
def show_date_pretty(self):
return self.show_date.strftime('%b %e %Y')
class Meta:
managed = False
db_table = 'shows'
I would like to create a model for Pets, pets table in the pets database, but I cannot figure out from the documentation how to be able to access the second database as a Model and then do a join on pet_id (foreign key), via the ShowPets model and the show_pets table (in the petshows database), to the pet_id (primary key) from pets table (in the pets database) to be able to pull things like pet_name, etc.
Can anyone give me any good hints or links on how to accomplish this? Thanks!