Very generous. Thank you. It looks as though you did get the model structure. Your first comment nails it because mixtures can have other mixtures as ingredients. I'm right into recursion nowadays :)

Mike

On 22/12/2016 7:48 PM, C. Kirby wrote:
Ill be honest, I'm still not getting how your models are structured. That is ok though, I can give you some pointers and hopefully that will be sufficient. I'll also be very explicit in describing the steps. Several are probably better as single orm calls. _meta docs at https://docs.djangoproject.com/en/1.10/ref/models/meta/

Hope this helps. If you have any clarifying questions I am happy to chime in more.

|
fromdjango.db.models importTextField
#First get all ingredients you are interested in (I am assuming that
#mixtures are only one level deep, if not then build a recursive call)
ingredients =Substance_Ingredients.objects.filter(substance=<parent substance>).values_list('ingredient.pk',flat=True)
#Get the substance instances for these
child_substances =Substance.objects.filter(pk__in =ingredients)
target_text =''
forcs inchild_substance:
#Get all OneToOne and OneToMany fields

    models =[
(f,f.model iff.model !=(SubstanceorSubstanceIngredient)elseNone)
forf inSubstance._meta.get_fields()
if(f.one_to_many orf.one_to_one)
andnotf.auto_created andnotf.concrete
]

    ingredient_text =''
forfield,model inmodels:
#Check the model for TextFields
        tfs =[f forf inmodel._met.get_fields()ifisinstance(f,TextField)]
#get the textfield attributes for your substance field
        field_text =[getattr(field,tf,'')fortf intfs]
#compile you text strings at the necessary levels
ingredient_text ='{}, {}'.format(ingredient_text,'.'.join(field_text))

    child_text ='{}: {}'.format(cs.substance_name,ingredient_text)
    target_text ='{}\n{}'.formate(target_text,child_text)

#Save you target text to the original substance
|


--
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 <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@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/b39f85e3-1ecc-4a2f-93e7-f6f426a63520%40googlegroups.com <https://groups.google.com/d/msgid/django-users/b39f85e3-1ecc-4a2f-93e7-f6f426a63520%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+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/d26f3359-1282-284b-910b-53a96706a060%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to