How to reallocate one object to another Django

How to reallocate one object to another Django

ยท

3 min read

So, the story begins long back ago when I was trying to move some of my lead from one campaign to another campaign. The concept was pretty much clear in my mind but still, I was worried to do so because of obvious reasons.

  1. what if it fails to do so
  2. what will be the correct way
  3. do I need to verify it again

So without much writing let's get how to do this in a proper way.

Main moto

The goal of this blog to get an understanding of how to move the object instance from one object to another.

As in getting the movable data and date from the client-side and fetching the data and after than moving the data into a new instance.

What we gonna do?

  • get the data from the client-side i.e. data-range, origin, destination
  • validate the data
  • move the data and sent back a response

Requirements

I'm using Python 3.6 and DRF 2 and to create API View, I'll be using the APIView for now but you can use any view as it is applicable for all.

1. Getting the data from client-side

from_date   = request.POST.get('from_date', '')
to_date       = request.POST.get('to_date', '')
origin          = request.POST.get('origin', '')
destination = request.POST.get('destination', '')

2. Validating the request

if not all(len(i) > 0 for i in [from_date, to_date, origin, destination]):
     data['msg'] = 'Please select all the value'
     data['status'] = 1
     return HttpResponse(json.dumps(data), content_type='application/json')

3. Converting the client-side date into Django format

start_datetime = datetime.strptime(from_date, format_str)
end_datetime = datetime.strptime(to_date, format_str)
start_dt = datetime(day=start_datetime.day, month=start_datetime.month, year=start_datetime.year,
                                hour=start_datetime.hour, minute=start_datetime.minute)
end_dt = datetime(day=end_datetime.day, month=end_datetime.month, year=end_datetime.year,
                              hour=end_datetime.hour, minute=end_datetime.minute)

4. Selecting the Moving campaign

campaign_to_extract_leads = Campaign.objects.get(slug=origin)

5. Now fetching the lead from campaign

total_leads_fetch = Lead.objects.filter(campaign=campaign_to_extract_leads,
                                                    is_active=True,
                                                    added_on__range=(start_dt, end_dt))

6. List out ids which need to be moved

total_leads_ids = list(total_leads_fetch.values_list('id', flat=True))

7. Select the campaign where you want to move the leads

campaign_of_moving_leads =Campaign.objects.get(slug=destination)

8. Now reallocating our object into a new instance

Lead.objects.filter(id__in=total_leads_ids).update(campaign=campaign_of_moving_leads,
                                                                   modified_on=datetime.now())

That's it, we have successfully moved the lead from the old campaign to the new campaign so using this way you'll easily able to move the old instance object into new.