Sorry about that ... here it is again.
- - - - - - - - - - - - - - - - - - -
Consider a chemical mixture with a bunch of ingredients. Both mixture
and ingredients are instances of the same class so they have the same
fields. They also have many related models, including 1:1, 1:n and n:m.
Each related model will have none or many TextField's.
The objective is to programmatically fill empty mixture text fields with
concatenated content from the ingredients. The concatenated content
would be separated by ingredient name titles for the user to deal with
the content more easily.
I don't necessarily need all mixture text fields filled this way but it
certainly makes sense for some. With a couple of related models I'd
concatenate all text fields, with most though I'd like to pick and
choose by field name and I'd ignore some models completely.
The following model method is working properly as described but it is
mostly boiler-plate. It also only covers the first few of a large number
of related models with text fields. If I keep using this technique it
will add hundreds of LOC. Yuk.
The question is how can I refactor this and make it generic? Perhaps
using the _meta API?
Any guidance appreciated
(In the abstract ancestor class of the Solid, Liquid and Gas classes)
def concat_fields(self, ingredients):
""" ingredients is a queryset of substance-to-substance m2m records.
A substance has one physical state object being gas, liquid or solid
each of which inherits from core_fields and the fields *here* we wish
to concatenate text from (at the moment) all come from core_fields.
"""
assert ingredients
# populate the list of ingredient physical state objects
state_objs = list()
for m2m in ingredients:
state_objs.append(m2m.ingredient.get_physical_state_object())
# get the text concatenated
if not self.stability_comment:
comment = ""
for obj in state_objs:
if obj.stability_comment:
name = obj.substance.name
text = obj.stability_comment
comment = "{0}\n{1}: {2}".format(comment, name, text)
comment = comment.strip()
if comment:
self.stability_comment = comment
if not self.reactivity:
comment = ""
for obj in state_objs:
if obj.reactivity:
name = obj.substance.name
text = obj.reactivity
comment = "{0}\n{1}: {2}".format(comment, name, text)
comment = comment.strip()
if comment:
self.reactivity = comment
if not self.reaction_hazards:
comment = ""
for obj in state_objs:
if obj.reaction_hazards:
name = obj.substance.name
text = obj.reaction_hazards
comment = "{0}\n{1}: {2}".format(comment, name, text)
comment = comment.strip()
if comment:
self.reaction_hazards = comment
if not self.avoid:
comment = ""
for obj in state_objs:
if obj.avoid:
name = obj.substance.name
text = obj.avoid
comment = "{0}\n{1}: {2}".format(comment, name, text)
comment = comment.strip()
if comment:
self.avoid = comment
if not self.incompatibilities:
comment = ""
for obj in state_objs:
if obj.incompatibilities:
name = obj.substance.name
text = obj.incompatibilities
comment = "{0}\n{1}: {2}".format(comment, name, text)
comment = comment.strip()
if comment:
self.incompatibilities = comment
Thanks
Mike
--
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/766455e3-24c9-8f1b-90ef-2466abcedc2b%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.