>> email to django-users+unsubscribe@googlegroups.com
>> <mailto:django-users+unsub...@googlegroups.com>.
>> To post to this group, send email to django...@googlegroups.com
>> <mailto: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/4441fec6-783c-41d8-a6bf-63499cdfece2%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/4441fec6-783c-41d8-a6bf-63499cdfece2%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>
>> For more options, visit https://groups.google.com/d/optout.
>
--
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 post to this group, send email to django...@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/370b1c12-8189-97d9-5ac6-8ca0f482a3bf%40djangodeployment.com.
On Sunday 12 February 2017 09:55:16 Mike Dewhirst wrote:
> A mixture has a number of ingredients and the proportions should
> (eventually) sum to 100%
>
> Ingredients are substances connected to the mixture - also a substance
> - via m2m relationships. Each m2m record carries an ingredient
> proportion value.
>
> If a proportion changes the sum total changes after saving the mixture
> but cannot be displayed in the parent until the following save.
The trick is not to focus on saving, but on providing a consistent state when loading the model instance, similar as to what ImageField does with it's width and height fields.
Here's an example:
https://gist.github.com/melvyn-sopacua/1b4e4585ab15ffc176798c083470cf04
--
Melvyn Sopacua
P.S. I left two bugs in there, for now I'll leave them in as an exercise for the reader.
--
Melvyn Sopacua
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