On Wednesday 15 February 2017 19:17:55 Mike Dewhirst wrote: > On 14/02/2017 12:14 PM, Melvyn Sopacua wrote: > > P.S. I left two bugs in there, for now I'll leave them in as an > > exercise for the reader. > > Three bugs ... and 2 questions: > > 1. You obviously forgot the sugar!
That's part of the first bug: it allows 2 of the same ingredients to be added to one mixture. Normally, you would merge them into one or disallow it with a unique_together on the through model. > 2. With that proportion of Earl Gray the tea would have been far too > strong. Haha, true. > 3. There might be 5% discrepancy in the total! Nope, should be correct. But the other bug is that it totals all mixtures, instead of only the instance of the mixture. This is because with through models you cannot use the related manager and I didn't add an explicit filter to compensate. > Now for the questions ... > > class CalculatedPercentageField(models.PositiveSmallIntegerField): > pct_model = None > pct_field_name = None > > def __init__(self, pct_model: models.Model, > pct_field_name='percentage', *args, **kwargs): > self.pct_model = pct_model > self.pct_field_name = pct_field_name > super().__init__(*args, **kwargs) > ... > > > 1. What does "pct_model: models.Model," do? I haven't seen that colon > before. This is me not having written python2 compatible code for a while. It's a python3.4+ supported type hint that makes it easier for the IDE to infer supported methods and properties. > 2. Will it work when used via the Admin and everything gets saved at > once in a single transaction? It should yes. Note that save is always one step behind what is presented. In practice this should not pose a problem and you might even put a save with update_fields in update_total() if old and new value differ, but this may trigger a can of worms if you ever put a save-related signal on the model. -- Melvyn Sopacua -- 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 post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/13283932.Y1uAX9IVB7%40devstation. For more options, visit https://groups.google.com/d/optout.