Search code examples
djangodjango-modelsdjango-rest-frameworkdjango-serializer

Django Models compare 2 model and filter


i want to get the model details with corresponding another model

my models.py

class Device(models.Model):
    DeviceName = models.CharField(max_length=50, null=True, default=None, blank=True)
    Camera = models.ForeignKey(Camera, on_delete=models.CASCADE, db_column='CameraId')

    class Meta:
        db_table = "Device"

class Camera(models.Model):
    CameraId = models.AutoField(primary_key=True, db_column='CameraId')
    CameraName = models.CharField(max_length=50)

    class Meta:
        db_table = "Camera"

i want my camera details

that is the camera details that not saved in Device foreign key (means the camera details that not used the foreign key for device)


Solution

  • Try to use device__isnull=True filter (Ref: Django isnull lookup)

    camera_without_devices = Camera.objects.filter(device__isnull=True)