Genel bir yabancı anahtarla ilişki kullanarak bir model seçmeye çalışıyorum, ancak beklendiği gibi çalışmıyor.Django genel yabancı anahtar ve select_related
Ben kodu
class ModelA(models.Model):
created = models.DateTimeField(auto_now_add=True)
class ModelB(models.Model):
instanceA = models.ForeignKey(ModelA)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
class ModelC(models.Model):
number = models.PositiveIntegerField()
bInstances = generic.GenericRelation(ModelB)
# Creating an instance of A and C
aInstance=ModelA.objects.create()
cInstance=ModelC.objects.create(number=3)
# Adding instance of C to the B_set of instance A
aInstance.modelb_set.add(content_object=cInstance)
# Select all ModelA instances that have C as content object? Does not work
whatIWant = ModelA.objects.filter(modelb__content_object=modelCInstance)
# Pseudo-solution, requires calling whatIWant.modelA
whatIWant = cInstance.bInstances.select_related("modelA")
Daha açıkçası daha iyi resimli ve anlaşılır olduğunu düşünüyorum, ben bu çizgi çalıştı istiyorum: ModelA.objects.filter(modelb__content_object=modelCInstance)
, görünüşe filtre ilişkileri content_object kullanarak desteklemez django.
Şimdiden teşekkürler!
Ne bekliyorsunuz? Varsa bir soruya cevap veremeyiz. –
Daha net olması için gönderiyi düzenledim, üzgünüm – Clash