lundi 31 août 2015

Fetch data from multiple tables in django views

In Django views, I want to fetch all details(Workeraccount.location,Workeravail.date, Workerprofile.*) for any particular wid=1.

SQL Query:

select * from Workeraccount,Workeravail,Workerprofile where Workerprofile.wid=1 and Workerprofile.wid=Workeravail.wid and Workeraccount.wid=Workerprofile.wid;

The corresponding models are as follows:

class Workeraccount(models.Model):
    wid = models.ForeignKey('Workerprofile', db_column='wid', unique=True)
    location = models.ForeignKey(Location, db_column='location')
    class Meta:
        managed = False
        db_table = 'workerAccount'

class Workeravail(models.Model):
    wid = models.ForeignKey('Workerprofile', db_column='wid')
    date = models.DateField()
    class Meta:
        managed = False
        db_table = 'workerAvail'

class Workerprofile(models.Model):
    wid = models.SmallIntegerField(primary_key=True)
    fname = models.CharField(max_length=30)
    mname = models.CharField(max_length=30, blank=True, null=True)
    lname = models.CharField(max_length=30)
    gender = models.CharField(max_length=1)
    age = models.IntegerField()
    class Meta:
        managed = False
        db_table = 'workerProfile'`



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire