Retrieving data from multiple tables

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Retrieving data from multiple tables

Suraj Thapa FC
This is my model

class courses(models.Model):
cid = models.UUIDField(default=uuid.uuid4, editable=True, primary_key=True)
course_title = models.CharField(max_length=255, default="")


class course_rating(models.Model):
cr_id = models.BigAutoField(primary_key=True)
cid = models.ForeignKey(courses,on_delete=models.CASCADE)
userid = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
rating = models.IntegerField(validators[MaxValueValidator(5),MinValueValidator(1)],default=1)




I want to fetch the course_title and cid from courses tables and want to perform the average rating of courses 
Can someone write the query set for the same

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPjsHcHXqr8%2Bnr91tA2Og%3DKpjeVGp8%2BFP%2BmsX9dbzuZxA5CHww%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving data from multiple tables

Sundararajan Seshadri
What you need to appreciate is that the foreign key field gives you the full record (corresponding to the foreign key).

So, if you fetch all records as course_rating.objects.all() then c.cid will also be an object giving you the corresponding foreign key record. So, you can access c.cid.cid and c.cid.course_title.(where c is one instance / record of the query set)

Some points:
(a) you are not following the convention. Not a crime. But if you follow the convention, it will prove convenient. For example. Courses will be the class name. (Table name can be anything)
(b) You named the foreign-key in the second table as cid, It is, again, not an issue but if you followed the convention, you could have called it as course so that field names could have been c.course.cid and c.course.course_title, probably more meaningful.

Again, convention is for convenience. 

Cheers.
====================================
On Wednesday, August 21, 2019 at 2:46:12 PM UTC+5:30, Suraj Thapa FC wrote:
This is my model

class courses(models.Model):
cid = models.UUIDField(default=uuid.uuid4, editable=True, primary_key=True)
course_title = models.CharField(max_length=255, default="")


class course_rating(models.Model):
cr_id = models.BigAutoField(primary_key=True)
cid = models.ForeignKey(courses,on_delete=models.CASCADE)
userid = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
rating = models.IntegerField(validators[MaxValueValidator(5),MinValueValidator(1)],default=1)




I want to fetch the course_title and cid from courses tables and want to perform the average rating of courses 
Can someone write the query set for the same

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f49660db-c157-43df-8563-3e9c2369a8dd%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving data from multiple tables

Suraj Thapa FC
Thanks... 

On Wed, 21 Aug, 2019, 6:24 PM Sundararajan Seshadri, <[hidden email]> wrote:
What you need to appreciate is that the foreign key field gives you the full record (corresponding to the foreign key).

So, if you fetch all records as course_rating.objects.all() then c.cid will also be an object giving you the corresponding foreign key record. So, you can access c.cid.cid and c.cid.course_title.(where c is one instance / record of the query set)

Some points:
(a) you are not following the convention. Not a crime. But if you follow the convention, it will prove convenient. For example. Courses will be the class name. (Table name can be anything)
(b) You named the foreign-key in the second table as cid, It is, again, not an issue but if you followed the convention, you could have called it as course so that field names could have been c.course.cid and c.course.course_title, probably more meaningful.

Again, convention is for convenience. 

Cheers.
====================================
On Wednesday, August 21, 2019 at 2:46:12 PM UTC+5:30, Suraj Thapa FC wrote:
This is my model

class courses(models.Model):
cid = models.UUIDField(default=uuid.uuid4, editable=True, primary_key=True)
course_title = models.CharField(max_length=255, default="")


class course_rating(models.Model):
cr_id = models.BigAutoField(primary_key=True)
cid = models.ForeignKey(courses,on_delete=models.CASCADE)
userid = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
rating = models.IntegerField(validators[MaxValueValidator(5),MinValueValidator(1)],default=1)




I want to fetch the course_title and cid from courses tables and want to perform the average rating of courses 
Can someone write the query set for the same

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f49660db-c157-43df-8563-3e9c2369a8dd%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPjsHcHkga3HSeBBTWSaRokmf-qNJq0txwOF6JvwjjsBOg_w_g%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving data from multiple tables

Amitesh Sahay
I am not much of a database guy. But if you wish to get something from different table, then there has to be some foreignkey there I guess your "courses" table should have CID as foreignkey. May be I am wrong. 


On Wednesday, 21 August, 2019, 08:26:52 pm IST, Suraj Thapa FC <[hidden email]> wrote:


Thanks... 

On Wed, 21 Aug, 2019, 6:24 PM Sundararajan Seshadri, <[hidden email]> wrote:
What you need to appreciate is that the foreign key field gives you the full record (corresponding to the foreign key).

So, if you fetch all records as course_rating.objects.all() then c.cid will also be an object giving you the corresponding foreign key record. So, you can access c.cid.cid and c.cid.course_title.(where c is one instance / record of the query set)

Some points:
(a) you are not following the convention. Not a crime. But if you follow the convention, it will prove convenient. For example. Courses will be the class name. (Table name can be anything)
(b) You named the foreign-key in the second table as cid, It is, again, not an issue but if you followed the convention, you could have called it as course so that field names could have been c.course.cid and c.course.course_title, probably more meaningful.

Again, convention is for convenience. 

Cheers.
====================================
On Wednesday, August 21, 2019 at 2:46:12 PM UTC+5:30, Suraj Thapa FC wrote:
This is my model

class courses(models.Model):
cid = models.UUIDField(default=uuid.uuid4, editable=True, primary_key=True)
course_title = models.CharField(max_length=255, default="")


class course_rating(models.Model):
cr_id = models.BigAutoField(primary_key=True)
cid = models.ForeignKey(courses,on_delete=models.CASCADE)
userid = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
rating = models.IntegerField(validators[MaxValueValidator(5),MinValueValidator(1)],default=1)




I want to fetch the course_title and cid from courses tables and want to perform the average rating of courses 
Can someone write the query set for the same

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f49660db-c157-43df-8563-3e9c2369a8dd%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPjsHcHkga3HSeBBTWSaRokmf-qNJq0txwOF6JvwjjsBOg_w_g%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/393408531.415507.1566408059600%40mail.yahoo.com.
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving data from multiple tables

Suraj Thapa FC
Yes it has
.... 
Can you write the query for the above question asked

On Wed, 21 Aug, 2019, 10:51 PM 'Amitesh Sahay' via Django users, <[hidden email]> wrote:
I am not much of a database guy. But if you wish to get something from different table, then there has to be some foreignkey there I guess your "courses" table should have CID as foreignkey. May be I am wrong. 


On Wednesday, 21 August, 2019, 08:26:52 pm IST, Suraj Thapa FC <[hidden email]> wrote:


Thanks... 

On Wed, 21 Aug, 2019, 6:24 PM Sundararajan Seshadri, <[hidden email]> wrote:
What you need to appreciate is that the foreign key field gives you the full record (corresponding to the foreign key).

So, if you fetch all records as course_rating.objects.all() then c.cid will also be an object giving you the corresponding foreign key record. So, you can access c.cid.cid and c.cid.course_title.(where c is one instance / record of the query set)

Some points:
(a) you are not following the convention. Not a crime. But if you follow the convention, it will prove convenient. For example. Courses will be the class name. (Table name can be anything)
(b) You named the foreign-key in the second table as cid, It is, again, not an issue but if you followed the convention, you could have called it as course so that field names could have been c.course.cid and c.course.course_title, probably more meaningful.

Again, convention is for convenience. 

Cheers.
====================================
On Wednesday, August 21, 2019 at 2:46:12 PM UTC+5:30, Suraj Thapa FC wrote:
This is my model

class courses(models.Model):
cid = models.UUIDField(default=uuid.uuid4, editable=True, primary_key=True)
course_title = models.CharField(max_length=255, default="")


class course_rating(models.Model):
cr_id = models.BigAutoField(primary_key=True)
cid = models.ForeignKey(courses,on_delete=models.CASCADE)
userid = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
rating = models.IntegerField(validators[MaxValueValidator(5),MinValueValidator(1)],default=1)




I want to fetch the course_title and cid from courses tables and want to perform the average rating of courses 
Can someone write the query set for the same

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f49660db-c157-43df-8563-3e9c2369a8dd%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPjsHcHkga3HSeBBTWSaRokmf-qNJq0txwOF6JvwjjsBOg_w_g%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/393408531.415507.1566408059600%40mail.yahoo.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPjsHcFC0fxEEJ0vfjkFH9go4K0AT9B%3D3rrJ9NTHXfH84AKqpw%40mail.gmail.com.