This is my statement:
prints_info= AvailablePrints.objects.select_related('picture_id').get(picture_id_id=sel_print)
This is the sql genereated by django:
SELECT "tshirts_availableprints"."available_print_id",
"tshirts_availableprints"."picture_id_id",
"tshirts_availableprints"."available_print_price",
"tshirts_availableprints"."available_print_name",
"tshirts_availableprints"."print_available",
"tshirts_pictures"."picture_id",
"tshirts_pictures"."picture_file_name",
"tshirts_pictures"."is_print"
FROM "tshirts_availableprints"
INNER JOIN "tshirts_pictures"
ON ("tshirts_availableprints"."picture_id_id" = "tshirts_pictures"."picture_id")
WHERE "tshirts_availableprints"."picture_id_id" = 6
LIMIT 21
My question: how do I access the field is_print?
I must have tried everything except the correct way.
Please post the Django models involved.
class AvailablePrints(models.Model):
available_print_id=models.AutoField(primary_key=True)
picture_id=models.ForeignKey("Pictures",to_field="picture_id",on_delete=models.CASCADE)
available_print_price=models.DecimalField(verbose_name="Printing price",max_digits=5,decimal_places=2)
available_print_name=models.CharField(max_length=100,verbose_name="Name of the print")
print_available=models.BooleanField(default=True)
def __str__(self):
return f"{self.available_print_name} - (${self.available_print_price})"
class Pictures(models.Model):
picture_id=models.AutoField(primary_key=True)
picture_file_name=models.CharField(max_length=100)
is_print=models.BooleanField(default=False, verbose_name="Het is een print")
def __str__(self):
return f"{self.picture_file_name}"
def images_path():
return os.path.join(settings.LOCAL_FILE_DIR,"images")
prints_info.picture_id.is_print
Thank you. Confirms what I thought it should be, but it didn’t work because of an error in the select_related statement. Then I got lost (again)
Just to clarify one common point of confusion - the select_related function is not required to access these related fields. It is a performance-enhancing function to prevent a second query from being issued, and not something that you must use.
I stumbled on it looking for something else while reading thru the API documentation. I have a lot of foreignkeys, so I thought it was a nice feature.
Only needed to get the grasp of it.
Thanks for your support.
Verzonden vanaf Outlook voor Android