Lleims
Lleims

Reputation: 1353

Obtain a specific data from my model - Django

I'm trying to understand how to obtain some data from my models.

I have a model like this,

class Device(models.Model):
    dev_id= models.CharField(max_length=16, primary_key=True, unique=True)
    dev_name = models.CharField(max_length=20, unique=True)

    fleet_id = models.ForeignKey(Fleet, on_delete=models.CASCADE)

def __str__(self):
    return self.dev_eui

If for example one of my devices has the dev_id like "lop7576", I want to obtain two data, its dev_name and its fleet_id.

If I do the following I obtain all the info from this device,

jq1 = Device.objects.filter(dev_id="lop7576")

But how can I obtain only this two values in string/int format directly?

Thank you very much.

Upvotes: 1

Views: 52

Answers (1)

JamJomJim
JamJomJim

Reputation: 26

You can use the .values() function like this:

jq1 = Device.objects.filter(dev_id="lop7576").values('dev_name', 'fleet_id')

Upvotes: 1

Related Questions