I would probably take my python script and convert it into one or more
model methods. In principle, if the data needs to be manipulated the
model should do the heavy lifting. It can be argued that database
procedures should do it but that is a rare art and the Django ORM (and
model methods) make it much easier to maintain.
At some point it needs to be saved to the database so the broad idea is
to put the user input into a model field then override the model's
save() method to call model methods to do the manipulation and perhaps
fill up some other fields before or add child records after the save
actually happens.
My typical pattern for such a save is ...
def save(self, *args, **kwargs):
# need to pass links across the super boundary
links = self.make_useful_links()
super(Substance, self).save(*args, **kwargs)
self.create_child_records(links)
... where Substance is the model name and 'links' is returned from a
model method. The call to super() triggers the normal Substance.save()
method. Because child records need the substance id for their FK, they
cannot be created until after the actual save() beyond which we can
guarantee the
substance.id is not None.
There are also pre_save and post_save signals available but I prefer the
above because I find it easier to see what the code is doing.
Your question about how to access the model values in your script is
solved if you embed the script in a model method. Every model method
should have 'self' as the first arg. 'self' is the model instance so
every field is available via self.field. For example if the Substance
name is used to generate a bunch of URLs pointing to a bunch of public
databases containing information on that substance ...
def make_useful_links(self):
links = list()
for database in databases:
links.append(self.assemble_url())
# self.assemble_url() has access to
self.name and
self.cas_no fields
return links
As it happens this could be done after the super() call but if you want
to save anything on the model, those fields have to be complete before
the save()
hth
> 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...@googlegroups.com
> <mailto:
django-users...@googlegroups.com>.
> To post to this group, send email to
django...@googlegroups.com
> <mailto:
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/394fd1d7-a2e2-4c59-ba9f-ba35d7eba9cd%40googlegroups.com
> <
https://groups.google.com/d/msgid/django-users/394fd1d7-a2e2-4c59-ba9f-ba35d7eba9cd%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit
https://groups.google.com/d/optout.