SELF JOIN queryset on joined table in Django?

纵然是瞬间 提交于 2020-01-05 04:07:22

问题


I want to make a self join on an joined table.

I have a queryset:

paymentsss = Transaction.objects.all().select_related('currency',
     'payment_source__payment_type','deal__service',
          'deal__service__contractor').order_by('-id')

Which is converted to:

 SELECT "processing"."transaction"."id", 
       "processing"."transaction"."currency_id", 
       "processing"."transaction"."deal_id", 
       "processing"."transaction"."payment_source_id", 
       "processing"."transaction"."payment_date", 
       "processing"."transaction"."amount", 
       "processing"."transaction"."status", 
       "processing"."transaction"."context", 
       "processing"."currency"."id", 
       "processing"."currency"."iso_name", 
       "processing"."currency"."minor_unit", 
       "processing"."deal"."id", 
       "processing"."deal"."service_id", 
       "processing"."deal"."currency_id",
       "processing"."service"."id",
       "processing"."service"."contractor_id",
       "processing"."service"."name", 
       "processing"."service"."description", 
       "processing"."contractor"."id", 
       "processing"."contractor"."name", 
       "processing"."payer_payment_source"."id", 
       "processing"."payer_payment_source"."payment_type_id", 
       "processing"."payer_payment_source"."source_details", 
       "processing"."payment_type"."id", 
       "processing"."payment_type"."name" 
  FROM "processing"."transaction" 
  LEFT OUTER JOIN "processing"."currency" 
    ON ("processing"."transaction"."currency_id" = "processing"."currency"."id") 
  LEFT OUTER JOIN "processing"."deal" 
    ON ("processing"."transaction"."deal_id" = "processing"."deal"."id") 
  LEFT OUTER JOIN "processing"."service" 
    ON ("processing"."deal"."service_id" = "processing"."service"."id") 
  LEFT OUTER JOIN "processing"."contractor" 
    ON ("processing"."service"."contractor_id" = "processing"."contractor"."id") 
  LEFT OUTER JOIN "processing"."payer_payment_source" 
    ON ("processing"."transaction"."payment_source_id" = "processing"."payer_payment_source"."id") 
  LEFT OUTER JOIN "processing"."payment_type" 
    ON ("processing"."payer_payment_source"."payment_type_id" = "processing"."payment_type"."id") 

This is a fragment from a table payment_source. Fields that contain bank_card joined with Transaction, but bank_card_details is not. They have same payer_id. It is necessary that these two lines are combined into one.How i can do this?

Models.py

class Transaction(models.Model):
    id = models.BigIntegerField(blank=True, null=False, primary_key=True)
    currency = models.ForeignKey(Currency, null=True, on_delete=models.CASCADE)
    deal = models.ForeignKey(Deal, null=True, on_delete=models.CASCADE)
    # service_instance = models.ForeignKey(ServiceInstance, null=True, on_delete=models.CASCADE)
    payment_source = models.ForeignKey(PayerPaymentSource, null=True, on_delete=models.CASCADE)
    payment_date = models.DateTimeField(blank=True, null=True)
    amount = models.IntegerField(blank=True, null=True)
    status = models.CharField(max_length=255, blank=True, null=True)
    context = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = '"processing"."transaction"'

class PayerPaymentSource(models.Model):
    id = models.BigIntegerField(blank=True, null=False, primary_key=True)
    # payer_id = models.BigIntegerField(blank=True, null=True)
    payment_type = models.ForeignKey(PaymentType, max_length=64, blank=True, null=True, on_delete=models.CASCADE)
    source_details = models.TextField(blank=True, null=True)  # This field type is a guess.

    class Meta:
        managed = False
        db_table = '"processing"."payer_payment_source"'

UPD: Based on the queryset of the django, I changed the request to fit my goals(only the last 3 lines are affected).How to transfer it to the Django ORM? I need it because in Django ORM convenient filters for searching.

 SELECT "processing"."transaction"."id",
       "processing"."transaction"."currency_id",
       "processing"."transaction"."deal_id",
       "processing"."transaction"."payment_source_id",
       "processing"."transaction"."payment_date",
       "processing"."transaction"."amount",
       "processing"."transaction"."status",
       "processing"."transaction"."context",
       "processing"."currency"."id",
       "processing"."currency"."iso_name",
       "processing"."currency"."minor_unit",
       "processing"."deal"."id",
       "processing"."deal"."service_id",
       "processing"."deal"."currency_id",
       "processing"."service"."id",
       "processing"."service"."contractor_id",
       "processing"."service"."name",
       "processing"."service"."description",
       "processing"."contractor"."id",
       "processing"."contractor"."name",
       "ps"."id",
       "ps"."payment_type_id",
       "ps"."source_details",
       "processing"."payment_type"."id",
       "processing"."payment_type"."name",
       "psc"."source_details"
FROM "processing"."transaction"
LEFT OUTER JOIN "processing"."currency" ON ("processing"."transaction"."currency_id" = "processing"."currency"."id")
LEFT OUTER JOIN "processing"."deal" ON ("processing"."transaction"."deal_id" = "processing"."deal"."id")
LEFT OUTER JOIN "processing"."service" ON ("processing"."deal"."service_id" = "processing"."service"."id")
LEFT OUTER JOIN "processing"."contractor" ON ("processing"."service"."contractor_id" = "processing"."contractor"."id")
LEFT OUTER JOIN "processing"."payer_payment_source" AS "ps" ON ("processing"."transaction"."payment_source_id" = "ps"."id")
LEFT OUTER JOIN "processing"."payment_type" ON ("ps"."payment_type_id" = "processing"."payment_type"."id")
LEFT OUTER JOIN "processing"."payer_payment_source" AS "psc" ON ("ps"."payer_id" = "psc"."payer_id") AND "psc"."payment_type_id" = 'bank_card_details'

来源:https://stackoverflow.com/questions/58641582/self-join-queryset-on-joined-table-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!