Re: Calculated Fields

2022-04-21 Thread Ahmedrufai Otuoze
My bad. I bypassed your message completely.

Have a great day!

On Wed, 13 Apr 2022 at 18:32, Kasper Laudrup  wrote:

> On 13/04/2022 16.51, Ahmedrufai Otuoze wrote:
> > This is not a spam. I just tok a course on that aspect and wanted to
> help.
> > No strings attached.
> >
> > I may have gone against the policy and that's because I wasn't aware.
> > My intentions were genuine...
> >
>
> You were not the one I was replying to so I don't understand why you are
> replying like you were advertising this.
>
> Harsh Jain has previously been spamming this mailing list and he's the
> one I was replying to.
>
> Kind regards,
>
> Kasper Laudrup
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8f8efa76-713f-86c8-ed18-188482af2882%40stacktrace.dk
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAA50Jbdy0HQrQhXNRk1o0KiCHHddGhYwSqX4zbBdRx3_%3D3s_1g%40mail.gmail.com.


Re: Calculated Fields

2022-04-13 Thread Kasper Laudrup

On 13/04/2022 16.51, Ahmedrufai Otuoze wrote:

This is not a spam. I just tok a course on that aspect and wanted to help.
No strings attached.

I may have gone against the policy and that's because I wasn't aware.
My intentions were genuine...



You were not the one I was replying to so I don't understand why you are 
replying like you were advertising this.


Harsh Jain has previously been spamming this mailing list and he's the 
one I was replying to.


Kind regards,

Kasper Laudrup

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8f8efa76-713f-86c8-ed18-188482af2882%40stacktrace.dk.


OpenPGP_0xE5D9CAC64AAA55EB.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: Calculated Fields

2022-04-13 Thread Ahmedrufai Otuoze
This is not a spam. I just tok a course on that aspect and wanted to help.
No strings attached.

I may have gone against the policy and that's because I wasn't aware.
My intentions were genuine...

On Wed, 13 Apr 2022 at 12:08, Kasper Laudrup  wrote:

> On 13/04/2022 07.20, harsh jain wrote:
> > Hi,
> > This course will help you surely - https://geekster.in/
> >
>
> Stop spamming. This is *not* the way to attract potential clients.
>
> I would never ever consider using your company for anything and I hope
> others feel the same.
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f4ac9610-cb05-21a0-ade5-260573bddb9e%40stacktrace.dk
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAA50JbdP%2Bx%3DYDtK1m5%2BhBXh798Lw-CwWd8rbR4xYS8_BJ38Ysg%40mail.gmail.com.


Re: Calculated Fields

2022-04-13 Thread Kasper Laudrup

On 13/04/2022 07.20, harsh jain wrote:

Hi,
This course will help you surely - https://geekster.in/



Stop spamming. This is *not* the way to attract potential clients.

I would never ever consider using your company for anything and I hope 
others feel the same.


--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f4ac9610-cb05-21a0-ade5-260573bddb9e%40stacktrace.dk.


OpenPGP_0xE5D9CAC64AAA55EB.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: Calculated Fields

2022-04-13 Thread harsh jain
Hi,
This course will help you surely - https://geekster.in/

On Monday, 11 April 2022 at 18:47:36 UTC+5:30 otuozeah...@gmail.com wrote:

> Hi Mike, 
>
> I can help you solve the problems.
> I can clone it from your repo and we'll work on it...
>
> What do you think?
>
> On Sun, Apr 10, 2022, 11:56 PM Mike Dewhirst  
> wrote:
>
>>
>>
>>
>>
>> --
>> (Unsigned mail from my phone)
>>
>>
>>
>>  Original message 
>> From: tech george  
>> Date: 11/4/22 03:50 (GMT+10:00) 
>> To: django...@googlegroups.com 
>> Subject: Calculated Fields 
>>
>> Hello,
>>
>> I am trying to calculate fields directly from a model but no luck. I want 
>> to get total_price from quantity, reoder_level and unit price. My code is 
>> abelow, Please advise.
>>
>> class Stock(models.Model):
>> unit_price = models.DecimalField(max_digits=10, decimal_places=2, 
>> default='0', blank=True, null=True)
>> quantity = models.IntegerField(default='0', blank=True, null=True)
>> total_price = models.DecimalField(max_digits=10, 
>> decimal_places=2,default=1)
>> reorder_level = models.IntegerField(default='0', blank=True, 
>> null=True)
>>
>>
>> def save(self, *args, **kwargs):
>> self.total_price = self.quantity + self.reorder_level * 
>> self.unit_price
>> super(Stock, self).save(*args, **kwargs)
>>
>>
>> What am i my doing wrong
>>
>> If it was my code, I would extract the calculation into a model method 
>> and call it from the save method as you are doing.
>>
>> That would make it easier to unit-test and perhaps discover that there 
>> should be parens around the addition.
>>
>> You also need to call save on your model to execute the code.
>>
>> Apart from that, at first glance your code should work.
>>
>>
>>
>> Regards,
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> -- 
>> 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...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com
>>  
>> <https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c08b95b5-92de-461e-bced-92896e1af66fn%40googlegroups.com.


Re: Calculated Fields

2022-04-11 Thread Ahmedrufai Otuoze
Hi Mike,

I can help you solve the problems.
I can clone it from your repo and we'll work on it...

What do you think?

On Sun, Apr 10, 2022, 11:56 PM Mike Dewhirst  wrote:

>
>
>
>
> --
> (Unsigned mail from my phone)
>
>
>
>  Original message 
> From: tech george 
> Date: 11/4/22 03:50 (GMT+10:00)
> To: django-users@googlegroups.com
> Subject: Calculated Fields
>
> Hello,
>
> I am trying to calculate fields directly from a model but no luck. I want
> to get total_price from quantity, reoder_level and unit price. My code is
> abelow, Please advise.
>
> class Stock(models.Model):
> unit_price = models.DecimalField(max_digits=10, decimal_places=2,
> default='0', blank=True, null=True)
> quantity = models.IntegerField(default='0', blank=True, null=True)
> total_price = models.DecimalField(max_digits=10,
> decimal_places=2,default=1)
> reorder_level = models.IntegerField(default='0', blank=True, null=True)
>
>
> def save(self, *args, **kwargs):
> self.total_price = self.quantity + self.reorder_level *
> self.unit_price
> super(Stock, self).save(*args, **kwargs)
>
>
> What am i my doing wrong
>
> If it was my code, I would extract the calculation into a model method and
> call it from the save method as you are doing.
>
> That would make it easier to unit-test and perhaps discover that there
> should be parens around the addition.
>
> You also need to call save on your model to execute the code.
>
> Apart from that, at first glance your code should work.
>
>
>
> Regards,
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com
> <https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAA50JbfaKcO55otZ1gg_0hh6ZW_iSFEiTjy5Q86bA69GKEQTKA%40mail.gmail.com.


Re: Calculated Fields

2022-04-11 Thread Jet Ezra
I think you can use the legendary BODMAS, and apply brackets
self.total_price = self.quantity+(self.reorder_level * self.unit_price)
That should work

On Mon, Apr 11, 2022 at 7:49 AM tech george  wrote:

> Hello Mike,
>
> Unfortunately it's not calculating the fields as I wanted, instead it's
> calculating as below;
>
> reorder_level * unit_price + quantity
>
> Which is wrong because I'm getting less amount.
>
> Regards
>
> On Mon, 11 Apr 2022, 01:56 Mike Dewhirst,  wrote:
>
>>
>>
>>
>>
>> --
>> (Unsigned mail from my phone)
>>
>>
>>
>>  Original message ----
>> From: tech george 
>> Date: 11/4/22 03:50 (GMT+10:00)
>> To: django-users@googlegroups.com
>> Subject: Calculated Fields
>>
>> Hello,
>>
>> I am trying to calculate fields directly from a model but no luck. I want
>> to get total_price from quantity, reoder_level and unit price. My code is
>> abelow, Please advise.
>>
>> class Stock(models.Model):
>> unit_price = models.DecimalField(max_digits=10, decimal_places=2,
>> default='0', blank=True, null=True)
>> quantity = models.IntegerField(default='0', blank=True, null=True)
>> total_price = models.DecimalField(max_digits=10,
>> decimal_places=2,default=1)
>> reorder_level = models.IntegerField(default='0', blank=True,
>> null=True)
>>
>>
>> def save(self, *args, **kwargs):
>> self.total_price = self.quantity + self.reorder_level *
>> self.unit_price
>> super(Stock, self).save(*args, **kwargs)
>>
>>
>> What am i my doing wrong
>>
>> If it was my code, I would extract the calculation into a model method
>> and call it from the save method as you are doing.
>>
>> That would make it easier to unit-test and perhaps discover that there
>> should be parens around the addition.
>>
>> You also need to call save on your model to execute the code.
>>
>> Apart from that, at first glance your code should work.
>>
>>
>>
>> Regards,
>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com
>> <https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
jet

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMjc0xW-v7Gjp-uH0UnM%2B0q46h%3DpkJpACUjNOTxUWvK8Rk3xCw%40mail.gmail.com.


Re: Calculated Fields

2022-04-11 Thread tech george
Hello Antonis,

Thanks for the advice and the solution,

That solved my issue.

Thanks again.

On Mon, Apr 11, 2022 at 9:04 AM Antonis Christofides <
anto...@antonischristofides.com> wrote:

> As was found out with the conversation with Mike Dewhirst, the error
> appears to be because of wrong understanding of operator precedence
> .
> That's easy to correct with parentheses.
>
> Except for that, I wouldn't do it that way; I wouldn't store the total
> price in the database, because it's likely to create problems. Models that
> have not (yet) been saved to the database will not have a total price.
> Models that have been loaded from the database, modified, but not (yet)
> updated in the database will have a wrong total price. Saving to the
> database with bulk operations or with plain SQL may result in no total
> price. These are all symptoms of storing redundant information in the
> database, which is something that should be avoided.
>
> Instead, I would do this:
> class Stock(models.Model):
> unit_price = models.DecimalField(max_digits=10, decimal_places=2,
> default='0', blank=True, null=True)
> quantity = models.IntegerField(default='0', blank=True, null=True)
> reorder_level = models.IntegerField(default='0', blank=True, null=True)
>
> @property
> def total_price(self):
> return (self.quantity + self.reorder_level) * self.unit_price
>
>
>
> On 10/04/2022 20.49, tech george wrote:
>
> Hello,
>
> I am trying to calculate fields directly from a model but no luck. I want
> to get total_price from quantity, reoder_level and unit price. My code is
> abelow, Please advise.
>
> class Stock(models.Model):
> unit_price = models.DecimalField(max_digits=10, decimal_places=2,
> default='0', blank=True, null=True)
> quantity = models.IntegerField(default='0', blank=True, null=True)
> total_price = models.DecimalField(max_digits=10,
> decimal_places=2,default=1)
> reorder_level = models.IntegerField(default='0', blank=True, null=True)
>
>
> def save(self, *args, **kwargs):
> self.total_price = self.quantity + self.reorder_level *
> self.unit_price
> super(Stock, self).save(*args, **kwargs)
>
>
> What am i my doing wrong
>
> Regards,
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8efea23c-52d8-6ac1-b94e-3cb9ad46d086%40antonischristofides.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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20FeUPLkT6wYn6%3DHvMDa8u9d62i4HEo6Zy410Xjyz%2B9%2Bdw%40mail.gmail.com.


Re: Calculated Fields

2022-04-10 Thread Antonis Christofides
As was found out with the conversation with Mike Dewhirst, the error appears to 
be because of wrong understanding of operator precedence 
. 
That's easy to correct with parentheses.


Except for that, I wouldn't do it that way; I wouldn't store the total price in 
the database, because it's likely to create problems. Models that have not (yet) 
been saved to the database will not have a total price. Models that have been 
loaded from the database, modified, but not (yet) updated in the database will 
have a wrong total price. Saving to the database with bulk operations or with 
plain SQL may result in no total price. These are all symptoms of storing 
redundant information in the database, which is something that should be avoided.


Instead, I would do this:

class Stock(models.Model):
    unit_price = models.DecimalField(max_digits=10, decimal_places=2, 
default='0', blank=True, null=True)

    quantity = models.IntegerField(default='0', blank=True, null=True)
    reorder_level = models.IntegerField(default='0', blank=True, null=True)

    @property
    def total_price(self):
    return (self.quantity + self.reorder_level) * self.unit_price



On 10/04/2022 20.49, tech george wrote:


Hello,

I am trying to calculate fields directly from a model but no luck. I want to 
get total_price from quantity, reoder_level and unit price. My code is abelow, 
Please advise.


class Stock(models.Model):
    unit_price = models.DecimalField(max_digits=10, decimal_places=2, 
default='0', blank=True, null=True)

    quantity = models.IntegerField(default='0', blank=True, null=True)
    total_price = models.DecimalField(max_digits=10, decimal_places=2,default=1)
    reorder_level = models.IntegerField(default='0', blank=True, null=True)


    def save(self, *args, **kwargs):
        self.total_price = self.quantity + self.reorder_level * self.unit_price
        super(Stock, self).save(*args, **kwargs)


What am i my doing wrong

Regards,

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8efea23c-52d8-6ac1-b94e-3cb9ad46d086%40antonischristofides.com.


Re: Calculated Fields

2022-04-10 Thread Mike Dewhirst

On 11/04/2022 2:48 pm, tech george wrote:

Hello Mike,

Unfortunately it's not calculating the fields as I wanted, instead 
it's calculating as below;

reorder_level * unit_price + quantity


Try using brackets like this ...

self.total_price = (self.reorder_level + self.quantity) * self.unit_price

Although I might use an error preventer. Perhaps like this ...

if self.unit_price and (self.reorder_level or self.quantity):
    self.total_price = (self.reorder_level + self.quantity) * 
self.unit_price





Which is wrong because I'm getting less amount.

Regards

On Mon, 11 Apr 2022, 01:56 Mike Dewhirst,  wrote:





-- 
(Unsigned mail from my phone)




 Original message 
From: tech george 
Date: 11/4/22 03:50 (GMT+10:00)
To: django-users@googlegroups.com
Subject: Calculated Fields

Hello,

I am trying to calculate fields directly from a model but no luck.
I want to get total_price from quantity, reoder_level and unit
price. My code is abelow, Please advise.

class Stock(models.Model):
    unit_price = models.DecimalField(max_digits=10,
decimal_places=2, default='0', blank=True, null=True)
    quantity = models.IntegerField(default='0', blank=True, null=True)
    total_price = models.DecimalField(max_digits=10,
decimal_places=2,default=1)
    reorder_level = models.IntegerField(default='0', blank=True,
null=True)


    def save(self, *args, **kwargs):
        self.total_price = self.quantity + self.reorder_level *
self.unit_price
        super(Stock, self).save(*args, **kwargs)


What am i my doing wrong

If it was my code, I would extract the calculation into a model
method and call it from the save method as you are doing.

That would make it easier to unit-test and perhaps discover that
there should be parens around the addition.

You also need to call save on your model to execute the code.

Apart from that, at first glance your code should work.



Regards,

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit

https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com

<https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit

https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com

<https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email&utm_source=footer>.

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com 
<https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com?utm_medium=email&utm_source=footer>.



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e6094b5b-ee18-a521-3ae3-7dfb7a4686b0%40dewhirst.com.au.


OpenPGP_signature
Description: OpenPGP digital signature


Re: Calculated Fields

2022-04-10 Thread tech george
Hello Mike,

Unfortunately it's not calculating the fields as I wanted, instead it's
calculating as below;

reorder_level * unit_price + quantity

Which is wrong because I'm getting less amount.

Regards

On Mon, 11 Apr 2022, 01:56 Mike Dewhirst,  wrote:

>
>
>
>
> --
> (Unsigned mail from my phone)
>
>
>
>  Original message 
> From: tech george 
> Date: 11/4/22 03:50 (GMT+10:00)
> To: django-users@googlegroups.com
> Subject: Calculated Fields
>
> Hello,
>
> I am trying to calculate fields directly from a model but no luck. I want
> to get total_price from quantity, reoder_level and unit price. My code is
> abelow, Please advise.
>
> class Stock(models.Model):
> unit_price = models.DecimalField(max_digits=10, decimal_places=2,
> default='0', blank=True, null=True)
> quantity = models.IntegerField(default='0', blank=True, null=True)
> total_price = models.DecimalField(max_digits=10,
> decimal_places=2,default=1)
> reorder_level = models.IntegerField(default='0', blank=True, null=True)
>
>
> def save(self, *args, **kwargs):
> self.total_price = self.quantity + self.reorder_level *
> self.unit_price
> super(Stock, self).save(*args, **kwargs)
>
>
> What am i my doing wrong
>
> If it was my code, I would extract the calculation into a model method and
> call it from the save method as you are doing.
>
> That would make it easier to unit-test and perhaps discover that there
> should be parens around the addition.
>
> You also need to call save on your model to execute the code.
>
> Apart from that, at first glance your code should work.
>
>
>
> Regards,
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com
> <https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com.


RE: Calculated Fields

2022-04-10 Thread Mike Dewhirst
--(Unsigned mail from my phone)
 Original message From: tech george  
Date: 11/4/22  03:50  (GMT+10:00) To: django-users@googlegroups.com Subject: 
Calculated Fields Hello,I am trying to calculate fields directly from a model 
but no luck. I want to get total_price from quantity, reoder_level and unit 
price. My code is abelow, Please advise.class Stock(models.Model):    
unit_price = models.DecimalField(max_digits=10, decimal_places=2, default='0', 
blank=True, null=True)    quantity = models.IntegerField(default='0', 
blank=True, null=True)    total_price = models.DecimalField(max_digits=10, 
decimal_places=2,default=1)    reorder_level = models.IntegerField(default='0', 
blank=True, null=True)    def save(self, *args, **kwargs):        
self.total_price = self.quantity + self.reorder_level * self.unit_price        
super(Stock, self).save(*args, **kwargs)What am i my doing wrongIf it was my 
code, I would extract the calculation into a model method and call it from the 
save method as you are doing.That would make it easier to unit-test and perhaps 
discover that there should be parens around the addition.You also need to call 
save on your model to execute the code.Apart from that, at first glance your 
code should work.Regards,



-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com.


Calculated Fields

2022-04-10 Thread tech george
Hello,

I am trying to calculate fields directly from a model but no luck. I want
to get total_price from quantity, reoder_level and unit price. My code is
abelow, Please advise.

class Stock(models.Model):
unit_price = models.DecimalField(max_digits=10, decimal_places=2,
default='0', blank=True, null=True)
quantity = models.IntegerField(default='0', blank=True, null=True)
total_price = models.DecimalField(max_digits=10,
decimal_places=2,default=1)
reorder_level = models.IntegerField(default='0', blank=True, null=True)


def save(self, *args, **kwargs):
self.total_price = self.quantity + self.reorder_level *
self.unit_price
super(Stock, self).save(*args, **kwargs)


What am i my doing wrong

Regards,

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com.


Calculated fields and ordering

2020-01-09 Thread Manos Zeakis
 

I have created a calculated field in models.py

 

<…>

@property

def final_score(self):

return (100.0 * self.a) /(1 + 0.1 * self.b)

<…>

 

 

And declare it in admin.py 

 

<…>

@admin.register(Project)

class TaskAdmin(admin.ModelAdmin):

fields = (<...>, 'final_score')

list_display = fields

<…>

 

Final_score field appears successfully in admin listview, but instead of 
“normal” fields it does not have an ordering option.

 

I searched for it and I found that calculated fields cannot be sorted 
because they do not actually exist in database (which is valid, but I am 
not sure that I can fully understand the connection between the two).

 

Moreover I cannot understand what is going on with the get_query workaround 
that is used in order to provide ordering for this field. By not 
understanding, I cannot adapt it to my implementation.

 

Could someone please give me an insight on why there are these difficulties 
and workarounds with calculated fields and ordering? Thank you

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/851b346d-b110-483c-8be6-d8f71958dea8%40googlegroups.com.


calculated fields with foreign keys

2012-09-20 Thread kloetpatra
Hi!
I have the following models:

class Player(models.Model):
name = models.CharField('Name', max_length=40)
pub_date = models.DateTimeField('date added', 
default=datetime.datetime.now)
def __unicode__(self):
return self.name
def goals(self):
g = 0
ms1 = Match.objects.filter(opp1=self)
ms2 = Match.objects.filter(opp2=self)
for m in ms1:
g += m.opp1_goals
for m in ms2:
g += m.opp2_goals
return g

class Match(models.Model):
opp1 = models.ForeignKey(Player)
opp2 = models.ForeignKey(Player)
opp1_goals = models.IntegerField("Goals P1")
opp2_goals = models.IntegerField("Goals P2")
pub_date = models.DateTimeField('date published', 
default=datetime.datetime.now)

The problem is when I want to display a statistics overview with all 
players there will be 2 DB-Queries per player only to get the "goals" 
property.
And I got more "calculated fields" like number of won matches, number of 
lost matches, etc...
There are 364 Match entries and 46 Players.
At my overview page I got over 2000 SQL queries which take over 6 seconds 
to load the page.

How can I speed up the process?
I don't want to save those "calculated values" in the database.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/RsK3HmYOBBwJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Sorting on calculated fields

2007-05-25 Thread Malcolm Tredinnick

On Fri, 2007-05-25 at 09:54 -0700, John M wrote:
> Tim,
> 
> great answer, but I'm thinking I might need to build a true queryset
> myself.
> 
> I was really hoping to hack the queryset model to allow this to happen
> in the django classes.

I answered this with a reasonably detailed explanation of how you would
do that the last time you asked this question (last Tuesday). So the
second time around you are getting different answers.

Regards,
Malcolm



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Sorting on calculated fields

2007-05-25 Thread John M

Tim,

great answer, but I'm thinking I might need to build a true queryset
myself.

I was really hoping to hack the queryset model to allow this to happen
in the django classes.

Unless I'm missing something, the examples given, can I use those
'basically' as querysets in my templates?  i.e. display, fields, etc?
I think so

it would be really cool if I could intercept the queryset.orderby()
routine to determine if its a calced field and sort internally, but
that would probably throw the whole django ORM into chaos, no?

I'll try some of these examples for now and see how they work, thanks
for everyone's response!

John


On May 24, 7:15 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> > If I have a model that returns calculated fields, how would I sort a
> > query set on them?
>
> > Assume the query sets are relatively small (< 100) for now.
>
> Give this condition (a small set of data) and no need to slice
> the results on the server based on this order, it can be done
> purely in python.  If the data-set grows larger, it will be more
> efficient to do some hacks to get this on the server side of
> things.  However...assuming those conditions:
>
> For the below example code, "foo_list" is a list of Foo objects
> with an "x" and a "y" property.  For this example,
> some_function() determines the magnitude-squared (x**2 + y**2)
>
> >>> foo_list = list(Foo.objects.all())
> >>> foo_list
>
> [, , , ,
> , , , ,
> , ]>>> # sort them based on "x"
> >>> foo_list.sort(cmp=lambda a,b: cmp(a.x, b.x))
> >>> foo_list
>
> [, , , ,
> , , , ,
> , ]>>> # sort them based on "y"
> >>> foo_list.sort(cmp=lambda a,b: cmp(a.y, b.y))
> >>> foo_list
>
> [, , , ,
> , , , ,
> , ]>>> # sort it by the results of the 
> some_function() call
> >>> foo_list.sort(cmp=lambda a,b: cmp(a.some_function(),
> b.some_function()))
> >>> foo_list
>
> [, , , ,
> , , , ,
> , ]
>
> Note that the Python sort() function
> 1) does the sort in-place and
> 2) does *not* return the results (returns None instead)
>
> Hope this helps,
>
> -tim


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Sorting on calculated fields

2007-05-24 Thread Mike Axiak

Sorry, I meant to send this to the list.

If it's small enough, you can just do a standard python sort:

def sort_function(x, y):
return cmp(x.calculated_field, y.calculated_field)

query_list = list(queryset)
query_list.sort(sort_function)

If you want to use a Schwartzian transform for efficiency, just put it
in a list:

query_list = [(x.calculated_field, x) for x in queryset]
query_list.sort()
query_list = [x[1] for x in query_list]

All of these solutions, however, do not scale as well as they would if
you could express them in SQL.

Cheers,
Mike Axiak

On May 24, 8:33 pm, John M <[EMAIL PROTECTED]> wrote:
> If I have a model that returns calculated fields, how would I sort a
> query set on them?
>
> Assume the query sets are relatively small (< 100) for now.
>
> Is this possible?
>
> Thanks
>
> john


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Sorting on calculated fields

2007-05-24 Thread Tim Chase

> If I have a model that returns calculated fields, how would I sort a
> query set on them?
> 
> Assume the query sets are relatively small (< 100) for now.

Give this condition (a small set of data) and no need to slice
the results on the server based on this order, it can be done
purely in python.  If the data-set grows larger, it will be more
efficient to do some hacks to get this on the server side of
things.  However...assuming those conditions:

For the below example code, "foo_list" is a list of Foo objects
with an "x" and a "y" property.  For this example,
some_function() determines the magnitude-squared (x**2 + y**2)

>>> foo_list = list(Foo.objects.all())
>>> foo_list
[, , , ,
, , , ,
, ]
>>> # sort them based on "x"
>>> foo_list.sort(cmp=lambda a,b: cmp(a.x, b.x))
>>> foo_list
[, , , ,
, , , ,
, ]
>>> # sort them based on "y"
>>> foo_list.sort(cmp=lambda a,b: cmp(a.y, b.y))
>>> foo_list
[, , , ,
, , , ,
, ]
>>> # sort it by the results of the some_function() call
>>> foo_list.sort(cmp=lambda a,b: cmp(a.some_function(),
b.some_function()))
>>> foo_list
[, , , ,
, , , ,
, ]

Note that the Python sort() function
1) does the sort in-place and
2) does *not* return the results (returns None instead)

Hope this helps,

-tim



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Sorting on calculated fields

2007-05-24 Thread John M

If I have a model that returns calculated fields, how would I sort a
query set on them?

Assume the query sets are relatively small (< 100) for now.

Is this possible?

Thanks

john


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---