Do some task using models in a view before a request is executed.

24 views
Skip to first unread message

Christian

unread,
Aug 2, 2018, 8:29:51 AM8/2/18
to Django users
Hi there,

I'm kind of a novice in django and like to call rfm_update before the get request in the RFM Viewset will executed, 
but no surprise the migrate doesn't work  for a new deployment.

What is the best practice to do something like rfm_update so that migrate works.

Thanks in advance,
Christian


def rfm_update():    
rfms = RFM.objects.all()
rfms.delete()
df_rfm = pd.DataFrame(list(Order.objects.all().values()))
rfm_data = processing(df_rfm, datetime_col='order_datetime', customer_id_col='customer_id').reset_index()
RFM.objects.bulk_create([RFM(customer_id=rec[0],
frequency=rec[1],
recency=rec[2],
T=rec[3]) for rec in rfm_data.values])    

class RFMViewSet(viewsets.ModelViewSet):
   rfm_update()      # Not good!
   queryset = RFM.objects.all()
   serializer_class = RFMSerializer
   lookup_field = 'customer_id'
   http_method_names = ['get']


Jason

unread,
Aug 2, 2018, 9:18:36 AM8/2/18
to Django users
DRF viewsets provide default implementations of list (GET resource/) and retrieve (GET resource/id).  You can just reimplement those methods and call `rfm_update` in them.

Christian

unread,
Aug 3, 2018, 8:59:08 AM8/3/18
to Django users
Thanks!
Reply all
Reply to author
Forward
0 new messages