Write object that is a Foreign to an object that its have also root Foreign

Hello All,

I have following models showed below, i would like to write an object that is Foreign to an object that its have also root Foreign

my issue is that i don’t know how to reference and write to specific object

for example i have value os_vendor=‘microsoft’ in a table HostOsScanned that related to host_ip_name =‘’ that related to table HostScanned that ForeignKey scan_name=‘11_08_2021office’ on table SiteAsses

Please advice


class SiteAssest(models.Model):
    scan_name = models.CharField(max_length=250,blank=False)
    site_name = models.CharField(max_length=250,blank=False)
    location_name = models.CharField(max_length=250,blank=False)
    # Geo location
    lon = models.FloatField()
    lat = models.FloatField()
    site_ip_range1 = models.CharField(max_length=250,blank=True)
    site_ip_range2 = models.CharField(max_length=250,blank=True)
    site_ip_range3 = models.CharField(max_length=250,blank=True)
    #new entry
    scan_time_start = models.DateTimeField(default=datetime.date.today())
    scan_timestr_start = models.CharField(max_length=250,blank=True)
    scan_time_end = models.DateTimeField(default=datetime.date.today())
    scan_timestr_end = models.CharField(max_length=250,blank=True)
    scan_elapsed = models.IntegerField(blank=True, null=True)
    scan_exit_resault = models.CharField(max_length=50,blank=True)
    scan_args = models.CharField(max_length=250,blank=True)
    nmap_version = models.CharField(max_length=50,blank=True)

    # end new entry

    last_scaned = models.DateField(auto_now_add=False, null=True)
    ('ID', 'IDLE'),
    ('RU', 'RUNNING'),
    current_status = models.CharField(max_length=2,choices=TASK_STATUS_CHOICES,default='IDLE')

    site_rank = models.IntegerField(null=True)

    scan = models.ForeignKey(ScanSet, on_delete=models.RESTRICT)
    scan_count = models.IntegerField(default=0)

    def __str__(self):
        return self.scan_name

class HostScanned(models.Model):
    scan_name = models.ForeignKey(SiteAssest, on_delete=models.CASCADE)
    host_ip_name = models.CharField(max_length=250,blank=True)
    resolved_hostname = models.CharField(max_length=250,blank=True)
    resolve_type = models.CharField(max_length=250,blank=True)
    mac_address = models.CharField(max_length=250,blank=True)
    mac_addr_type = models.CharField(max_length=250,blank=True)
    mac_vendor = models.CharField(max_length=250,blank=True)
    host_state = models.CharField(max_length=50,blank=True)
    host_state_method = models.CharField(max_length=50,blank=True)
    host_state_ttl = models.CharField(max_length=50,blank=True)

    def __str__(self):
        return str(self.host_ip_name)

class HostOsScanned(models.Model):
    host_ip_name = models.ForeignKey(HostScanned, on_delete=models.CASCADE)
    os_type = models.CharField(max_length=50,blank=True)
    os_accuracy = models.CharField(max_length=50,blank=True)
    os_name = models.CharField(max_length=300,blank=True)
    os_fingerprint = models.CharField(max_length=300,blank=True)
    os_family = models.CharField(max_length=100,blank=True)
    os_vendor = models.CharField(max_length=100,blank=True)
    os_cpe = models.CharField(max_length=300,blank=True)

    def __str__(self):
        return str(self.os_type)

To make sure I understand what you’re asking:

You have three tables

HostOsScanned has a ForeignKey to HostScanned. HostScanned has a ForeignKey to SiteAssest.

If you have an instance of HostOsScanned, you refer to both HostScanned and SiteAssest through the foreign keys.

example, assume host_os_scanned is an instance of HostOsScanned:
site_assest = host_os_scanned.host_ip_name.scan_name

(The field host_ip_name in HostOsScanned is your link to HostScanned, and scan_name is your link between HostScanned and SiteAssest.)

1 Like

Exactly same as you described

So then is there still an outstanding question, or does my previous reply cover it?

1 Like

If you can give me an example how to make the HostOsScanned object write with reference to its related tables
That will be great
Thanks you very much

A ForeignKey field is set the same way you set any other variable, the only difference is that the value being set is a reference to an instance of the other object.

example, if host_os_scanned is an instance of HostOsScanned and host_scanned is an instance of HostScanned, then:
host_os_scanned.host_ip_name = host_scanned
sets the reference between the two.

1 Like

Thanks you very much