Monday, June 28, 2021

Re: using .annotate on a queryset and output field is a result of function

this looks good, thanks

On Thu, Jun 17, 2021 at 6:08 PM Nikeet NA <nikeet@exiverlabs.co.in> wrote:
Its not the perfect method you can optimize ot more if you want. 
You can prefer this Link

On Thu, 17 Jun 2021 at 18:06, Nikeet NA <nikeet@exiverlabs.co.in> wrote:
You can do it like this :

class TransactionEntry(models.Manager):
       def get_total_prices(self):
             return self.queryset().defer("users").annotate.(total_price=F("num_shares")*F("price_per_share"))

On Thu, 17 Jun 2021 at 15:31, VISHESH MANGLA <f20170548@pilani.bits-pilani.ac.in> wrote:
what about overwriting the manager? How to make the manager auto-add those computed properties ?

On Thursday, June 17, 2021 at 8:50:27 AM UTC+5:30 Nikeet NA wrote:
You cannot use properties in django orm , it does not allow that.
TransactionEntry.objects.defer("users").annotate(total_price=F("num_shares")*F("price_per_share")).values('total_price')
On Thursday, 17 June 2021 at 03:23:01 UTC+5:30 VISHESH MANGLA wrote:
Hello,

I wanted to know that how for the model below,  how can I get a queryset with  all the fields excluding the `user` and including the  output of get_output_field? Please avoid hardcoding.
One way is

`TransactionEntry.objects.defer("users").annotate(total_price=F("num_shares")*F("price_per_share"))`

but that is undesirable. I just want to use that function on the model and not hard code the formula.
Thanks,



class TransactionEntry(models.Model):
    """Model to save data of users who want to buy/sell shares"""

    user = models.ForeignKey(
        to=Relative,
        on_delete=models.CASCADE,
        related_name="transaction_entry",
    )

    num_shares = models.PositiveIntegerField(null=Falseblank=False)
    date_of_trade = models.DateField(null=Falseblank=False)
    transaction_type = models.CharField(
        max_length=1,
        choices=TransactionType.choices,
        default=TransactionType.SELL,
        null=False,
    )
    price_per_share = models.FloatField(null=Falseblank=False)

    def get_total_price(self):
        return self.num_shares * self.price_per_share

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/35d917c8-e321-42eb-8050-f7849c0374e8n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFOOHnP8Y285E9HztCq8LUcTwZ3G%2BDT2T%2BgB35j_JfdK8va6ow%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALG_zn4xVxRhQPFD%2Bq%2BcC%3DAiFNfU3ZSxLgmPXhQu%3DQ-_hySbvQ%40mail.gmail.com.

No comments:

Post a Comment