Django - how to retrieve data from two models (at the same time) related with OneToOne relation?
In Django I have two models:
class Car(models.Model):
model_name = CharField(...)
make_name = CharField(...)
car_exterior_color= = CharField( CHOICES )
class CarWash(models.Model):
car_washed = BooleanField(...)
wash_date = DateTimeField(...)
added_wax = BooleanField(...)
car = models.OneToOneField(Car, on_delete=models.CASCADE, verbose_name="Car washed")
I need to show such table in view, that retrieves data from two models:
model_name | make_name | car_ext_col | car_washed ? | added_wax ? | wash date |
---|---|---|---|---|---|
IS | Lexus | blue | Yes | No | 2023-02-02 |
G37 | Infiniti | white | No | No | -- |
RX | Lexus | red | Yes | No | 2023-02-02 |
Corolla | Toyota | green | No | No | -- |
Tundra | Toyota | blue | Yes | Yes | 2023-02-02 |
Q70 | Infiniti | yellow | Yes | Yes | 2023-02-03 |
Civic | Honda | black | Yes | No | 2023-02-03 |
Malibu | Chevrolet | red | Yes | Yes | 2023-02-04 |
GS | Lexus | yellow | Yes | No | 2023-02-04 |
Q30 | Infiniti | white | No | No | -- |
What should I write in views.py?
How to retrieve data from two models to get table as above?
Should I change my model to get table as required?
I think, you have to create separate app to join 2 data models, and inside this app create model which combines your 2 data models, and create view etc for that data model.