https://prefect.io logo
Title
p

Phoebe Bright

11/11/2019, 1:58 PM
Hi, I've just started using Prefect with Django and I have a couple of questions 1. Is there some way of running a flow that includes a Django context so I can access the model methods? I can use API calls instead. Is this a better way to do it? 2. How to I trigger the correct workflow? The overall flow is like this: Check to see if any new records have been added to a Django model/postgres table For each new record, work out what type it is and therefore which workflow to run, eg if type == 1 flow1.run() @taska @taskb @taskc elif type == 2 flow2.run() @taska @taskz What is the best approach? - Should this all be in Prefect - not sure how to do that - Should I have a piece of external code that checks for new records and what type they are and then calls the correct prefect flow with the parameter of the new record - Is there a better way of doing this!?
j

Jeremiah

11/11/2019, 2:08 PM
Hey @Phoebe Bright, welcome to the community! I am not familiar with Django myself so I’m not sure I can completely answer question 1. Is the Django context a global object? Perhaps you can access it like you normally would. Maybe someone else will be more helpful! As for question 2, if your set up is working for you, then my recommendation is usually to stick with it 🙂. Prefect’s pretty flexible, and running two flows in response to a condition is totally fine. If you did want to move the entire thing into a single Flow, you could use Prefect’s
ifelse()
or
switch()
operators to create conditional branches. By combining these with a
Parameter
to receive the input
type
, you could have a single workflow that you’d call like
flow.run(type=type)
. The decision of whether to do this depends on how your flows are structured. For example, if your flows share some task logic but not the same dependencies (in your example, task “A” is in both flows but followed by different tasks), then it’s best to keep them as two separate flows because setting up the conditional dependencies could get messy. If, on the other hand, the branches following the condition are more distinct (a → b → c on one side on x → y → z on the other), then the full-Prefect approach is easier. However as I said in the simple case you presented, nothing wrong with your approach and in fact it might be the simplest way to get going!
p

Phoebe Bright

11/11/2019, 2:16 PM
Thanks Jeremiah.
I'm not sure I understand Django from the outside, hopefully someone else will answer that part of the question.
In terms of flow, I think I will go with creating a separate flow for each type of document as the list of tasks could be very different. I can put the common tasks in a tasks.py file and import them into each custom flow as required. Will experiment with that anyway. Thanks for the input.
j

Jeremiah

11/11/2019, 2:20 PM
That sounds great — let us know how we can help!
p

Phoebe Bright

11/11/2019, 2:43 PM
Thanks - I will!