On 20/02/2019 6:39 pm, Ousmane Baricisse wrote:
Hi,
I would like to implement a web app using reactjs and django. I have a python script which does some sort of optimization and stores the final output in a list and returns it. Right now everything is static in the python script and I would like to make it dynamic so that i can get a user input (from a web browser) and with that, run the python script internally, and pass back the data to the django models, which will update the values in the API.
so far, I know how to:
build a REST API with django,
get user input using reactjs,
make post and get request to get data from the API or update the API data from user input, However, I do not know how to access the models values in my python script, run the python script with django, and update the API with so that it contains the output in order for me to be able fetch the output with react.

I am not sure if im doing this the easiest way, as it sounds so complicated, but I am open to suggestions on better ways of doing this.

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+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/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.

--
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/48fd56c6-3636-149e-f942-3c96ff02933c%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to