Can we create a temporary table or views to store the data from 3 or 4 different tables and send a flattened JSON instead of nested JSON to the frontend in Django?
My model is:
class Place(models.Model):
id = models.IntegerField(primary_key=True)
location = models.CharField(max_length=100)
class Meta:
db_table = 'place'
managed=False
class Session(models.Model):
id = models.IntegerField(primary_key=True)
place = models.ForeignKey(Place,related_name='session',on_delete=models.CASCADE, null=True)
start = models.DateField(auto_now=True)
counts = models.IntegerField()
class Meta:
db_table = 'session'
managed=False
class Animal(models.Model):
id = models.IntegerField(primary_key=True)
sess = models.ForeignKey(Session,related_name='details',on_delete=models.CASCADE, null=True)
type = models.CharField(max_length=100)
is_active = models.BooleanField()
length = models.DecimalField(max_digits=6, decimal_places=2)
class Meta:
db_table = 'animal'
managed=False
The flatten output I am trying is:
[
{
"location": "Loc 1",
"session_start": "2021-01-01",
"session_count": 900,
"session_details_id": 1,
"session_details_length_max": "22.00",
"session_details_length_min": "10.00",
"session_details_length_avg": "16.43",
"session_details_length_std": "16.00",
"session_details_is_active": false,
"session_details_type": "dog"
},
"location": "Loc 1",
"session_start": "2021-01-02",
"session_count": 400,
"session_details_id": 2,
"session_details_length_max": "19.00",
"session_details_length_min": "12.00",
"session_details_length_avg": "15.43",
"session_details_length_std": "13.00",
"session_details_is_active": false,
"session_details_type": "dog"
}
]
Instead of nested JSON data that I am currently getting The nested JSON data is
[
{
"location": "Loc 1",
"session": [
{
"start": "2021-01-01",
"count": 600,
"details": [
{
"id": 1,
"length_max": "15.00",
"length_min": "10.00",
"length_avg": "12.00",
"length_std": "13.00",
"is_active": false,
"type": "dog"
}
]
},
{
"start": "2021-01-02",
"count": 400,
"details": [
{
"id": 2,
"length_max": "19.00",
"length_min": "12.00",
"length_avg": "15.00",
"length_std": "13.00",
"is_active": true,
"type": "dog"
}
]
},
{
"start": "2021-01-01",
"count": 300,
"details": [
{
"id": 13,
"length_max": "22.00",
"length_min": "20.00",
"length_avg": "20.00",
"length_std": "22.00",
"is_active": false,
"type": "dog"
}
]
}
]
}
]
Like we do in SQL, can we create temporary tables or views in Django to store and flattened?