Форум сайта python.su
Есть модель:
class Photo(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
object = generic.GenericForeignKey('content_type', 'object_id')
image = models.ImageField(upload_to=get_upload_to, max_length=1024)
class Common(models.Model):
# Поля...
photo = generic.GenericRelation(Photo)
class Class_common(Common):
# Поля...
class Class_car(Class_common):
# Поля...
Class_car.objects.get(pk=1).photo.all()
SELECT "media_photo"."id",
"media_photo"."content_type_id",
"media_photo"."object_id",
"media_photo"."image",
FROM "media_photo"
WHERE ("media_photo"."object_id" = 1 AND "media_photo"."content_type_id" = 41 )
Class_car.objects.filter(photo__isnull=False)
SELECT DISTINCT T6."id",
"advert_class_car"."class_common_ptr_id",
FROM "advert_class_car"
INNER JOIN "advert_class_common" ON ("advert_class_car"."class_common_ptr_id" = "advert_class_common"."common_ptr_id")
INNER JOIN "common_common" ON ("advert_class_common"."common_ptr_id" = "common_common"."id")
INNER JOIN "media_photo" ON ("common_common"."id" = "media_photo"."object_id")
INNER JOIN "common_common" T6 ON ("advert_class_car"."class_common_ptr_id" = T6."id")
WHERE ("media_photo"."id" IS NOT NULL AND "media_photo"."content_type_id" = 130 )
Офлайн