select_related is used so whenever a defined foreign key accessed, it will be included in the first query and won't issue another query to db. But I have a case when only few fields are needed. Here is the example
class PMapKomoditi(models.Model):
id = models.IntegerField(primary_key=True)
komoditi_fk = models.ForeignKey(PMastKomoditi, models.RESTRICT, db_column='id_komoditi')
kd_komoditi = models.CharField(max_length=2)
nm_komoditi = models.CharField(max_length=50, blank=True, null=True)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
// ...
class PMastKomoditi(models.Model):
id = models.AutoField(primary_key=True)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
// ...
I only need kd_komoditi & komoditi_fk properties, so I do query the following:
my_query = PMapKomoditi.objects.select_related(
'komoditi_fk'
).Only(
'kd_komoditi',
'komoditi_fk'
)
Do I need to include the select_related or it's already covered with only()?