Search code examples
djangodjango-modelsdjango-viewsdjango-filterdjango-filters

Django - filtering on foreign key usin an attribut


i have 2 foreingkey , one from the User and the other one from Device.

class checkn(models.Model):
    user = models.ForeignKey(User, null=True,on_delete= models.SET_NULL)
    devices = models.ForeignKey(Device, null=True,on_delete= models.SET_NULL)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    ch = models.CharField(max_length=1000, null=True)

he's using the Device ID for the search and i want to use the IP adresse : enter image description here

this is the device model :

class Device(models.Model):
    hostname = models.CharField(max_length=200, null=True)
    password = models.CharField(max_length=200, null=True)
    type = models.CharField(max_length=200,choices=dtype, null=True)
    ipadress = models.CharField(max_length=200, null=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

any advices ?


Solution

  • If you want the ip address to show in the search field do

    class Device(models.Model):
       ...
       def __str__(self):
          return self.ipaddress