Я делаю копию объекта. Он работает, за исключением поля времени, связанного с полем дня, которое я не могу скопировать. Что может быть не так с моим кодом?
models.py
class Facility(models.Model):
id = models.UUIDField(primary_key=True, default=uuid_generate_v1mc, editable=False)
name = models.CharField(max_length=250, blank=True, null=True)
description = models.CharField(max_length=1000, blank=True, null=True)
class FacilityDaySlot(models.Model):
day_slot_id = models.UUIDField(primary_key=True, default=uuid_generate_v1mc, editable= False)
facility = models.ForeignKey(Facility, on_delete=models.CASCADE, blank=True, null=True, related_name='days')
open_time = models.TimeField(blank=True, null=True)
close_time = models.TimeField(blank=True, null=True)
class FacilityTimeSlot(models.Model):
id = models.UUIDField(primary_key=True, default=uuid_generate_v1mc, editable=False)
day_slot = models.ForeignKey(FacilityDaySlot, on_delete=models.CASCADE, blank=True, null=True, related_name="times")
start_slot_time = models.TimeField(blank=True, null=True)
end_slot_time = models.TimeField(blank=True, null=True)
views.py
@api_view(["POST"])
@permission_classes((AllowAny,))
def facility_clone(request):
""" Generate the duplicate Facility with out publish, FacilitySport, FacilityLevel"""
facility_id = request.data.get("facility_id", None)
if facility_id:
try:
current_facility = get_object_or_404(Facility, id=facility_id)
old_facility_id = current_facility.id
current_facility.id = None
current_facility.is_draft = True
# do not publish for duplicated facility
current_facility.is_published = False
current_facility.save()
new_facility = get_object_or_404(Facility, id=current_facility.id)
current_facility = get_object_or_404(Facility, id=old_facility_id)
for day_slot in FacilityDaySlot.objects.filter(facility_id=current_facility.id):
day_slot.day_slot_id = None
day_slot.facility_id = new_facility.id
day_slot.save()
for time_slot in day_slot.times.all():
time_slot.id = None
time_slot.day_slot = day_slot
time_slot.save()
message = {
'message': 'duplicate facility created and id is {}'.format(new_facility.id)
}
return Response(message, status=status.HTTP_201_CREATED)
except Exception as e:
logger.error('error while creating duplicate event %s', e)
message = {
'message': str(e)
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
else:
message = {
'message': 'facility_id required in request to duplicate'
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
Я получаю вывод как:
"day_slots": [
{
"day_slot_id": "6fea1b94-4d62-11ea-96b5-6095cf4fade1",
"time_slots": [],
Почему временные интервалы пусты?