1

I want to add one subquery to my query. And I created a @property in Transaction. Found on the Internet that this is what I need. But I do not fully understand how they work. How to use it?

views.py(Query)

paymentsss = Transaction.objects.all().select_related('currency',
       'payment_source__payment_type',
     'deal__service__contractor',).

models.py

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"'


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)

    @property
    def bank_card_details(self):
        return PayerPaymentSource.objects.filter(self.payment_source.source_details,
                                                 payment_type='bank_card_details')

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

UPD: print(payment.bank_card_details) works, but it creates a lot of similar queries. How to fix it?

enter image description here

  • Maybe this answer will help you: https://stackoverflow.com/a/17330273/6836173 – wfehr Nov 07 '19 at 07:07
  • Possible duplicate of [How does the @property decorator work?](https://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work) – Ruelos Joel Nov 07 '19 at 07:26

1 Answers1

1

The @property decorator is just a convenient way to call the property() function, which is built in to Python. This function returns a special descriptor object which allows direct access to the method's computed value.

For example in your view

obj = Transaction.objects.get(pk=pk)
#now you can get the bank_card_details like this:
print(obj.bank_card_details)
arjun
  • 7,230
  • 4
  • 12
  • 29
  • What's the difference between `obj.bank_card_details()` and `obj.bank_card_details`? I found the first option on the Internet and it displays the data that I need, but it creates a lot of duplicate queries. The second one does not create duplicates, but outputs something like this: `` –  Nov 07 '19 at 06:56
  • @Fyzzzzzysssssss seems like that you didn't use `@property` in your case. At least the output with ` – wfehr Nov 07 '19 at 07:05
  • @wfehr It seems I accidentally deleted a `@property`, thanks. –  Nov 07 '19 at 07:18