Assume u have two models. Let's name them computer and log_file.
I want to make a join so that a always display all computer objects, but if there is a related log_file object it is also added to the query result.
Which is the best way to achieve this? Relationship: one Computer has one log file. But it is possible it's not uploaded yet to the database. Only gets uploaded when my script throws an error.
Sorry for my beginner question, I'm new to Django.
following simplified models as an example:
Model 1: computer
- id (pk)
- computer name
- mac address (unique)
Model 2: log file / max one for each computer
id (pk)
mac address
text field
required query: list of all computers and also the log file if there is any in the database. The join is made by the value of the mac address.
Think the SQL query would look like the following. But I am not able to translate this with the ORM.
- SELECT *
- FROM computer
- LEFT JOIN log ON computer.mac_address = log_file.mac_address;
Thank you!