0

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!

peter
  • 95
  • 1
  • 11
  • [https://stackoverflow.com/questions/21271835/left-join-django-orm?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa](https://stackoverflow.com/questions/21271835/left-join-django-orm?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) maybe this can help – KAYKISIZ May 21 '18 at 19:20

1 Answers1

0

I found a work around by using a calculated field. Check the following link:

How to add a calculated field to a Django model

Everything below @property

I added some extra code to my model:

class ConnecThor(models.Model):
# Incrementing ID (created automatically)
hostname = models.CharField(max_length=40)
MAC_address = models.CharField(max_length=17,
                               default='00.00.00.00.00.00', help_text="Example format: 255.255.255.255",
                               primary_key=True)
IP_address = models.CharField(max_length=15, default='000.000.000.000', help_text="Example format: 192.148.0.1")
config_version = models.CharField(max_length=10, default='000')
creation_date = models.DateTimeField(auto_now_add=True, null=True,
                                     editable=False)  # timezone.now() gedaan tijdens make migrations

@property
def log_file(self):
    try:
        return LogFiles.objects.get(MAC_address=self.MAC_address)
    except LogFiles.DoesNotExist:
        print('logfile not found')

class Meta(object):
    db_table = 'connecthor'  # table name

And to my view:

                             {% if  connecthor.log_file %}
                                    {% if connecthor.log_file.viewed > 0 %}
                                        <td><a  href="{% url 'show_error_log' connecthor.MAC_address %}"  class="badge badge-teal">Approved</a></td>
                                    {% else %}
                                        <td><a href="{% url 'show_error_log' connecthor.MAC_address %}" class="badge badge-danger">Failed update</a></td>
                                    {% endif %}
                                {% else %}
                                 <td><label class="badge badge-warning">Not Found</label></td>
                                {% endif %}
peter
  • 95
  • 1
  • 11