Ayoub Bsl
Ayoub Bsl

Reputation: 159

How To Select Data From Two Tables In Django?

Using django I have created two classes, but I have a problem in relation between tables and selecting rows:

class ayoub (models.Model):
    name=models.CharField(max_length=200)
    sname=models.CharField(max_length=200)

class sabri(models.Model):
    a=models.ForeignKey(ayoub, on_delete=models.CASCADE)
    name=models.CharField(max_length=200)
    sname=models.CharField(max_length=200)

I want to display all the attributes of sabri with the attribute name from class ayoub not the primary key (id)

Upvotes: 0

Views: 644

Answers (1)

Exprator
Exprator

Reputation: 27513

sabri.objects.filter(a__name='something').values('name','sname')

if you need name and sname from ayoub class you can try this query

sabri.objects.filter(a__name='something').values('name','sname','a__name','a__sname')

Upvotes: 1

Related Questions