I have an Event model, and each event will have different shows.

    class Event(models.Model):
        title = models.CharField(max_length=200)

    class Show(models.Model):
        event = models.ForeignKey(Event, on_delete=models.CASCADE)
        date_time = models.DateTimeField(unique=True)

    class Ticket(models.Model):
        show = models.ForeignKey(Show)
        seat = models.ForeignKey(Seat)

        class Meta:
            unique_together = ('show', 'seat')

I need to create shows based on the start date and end date provide by the 
user. Suppose this is a JSON post:

    {
        "event_id": 1,
        "start_date": "2018-02-16",
        "end_date": "2018-02-20",
        "time_list": ["11:00 AM", "8:00 PM"]
    }

>From the above JSON example, I need to create Show starting like this:

    # Start with the start_date as the date, and for each time from the 
time_list
    Show.objects.create(
        event = 1,
        date_time = datetime.strptime('2018-02-16 11:00 AM', "%Y-%m-%d 
%I:%M %p")
    )
    Show.objects.create(
        event = 1,
        date_time = datetime.strptime('2018-02-16 8:00 PM', "%Y-%m-%d %I:%M 
%p")
    )
    # Next date after the start_date, i.e., 16+1 = 17
    Show.objects.create(
        event = 1,
        date_time = datetime.strptime('2018-02-17 8:00 PM', "%Y-%m-%d %I:%M 
%p")
    )
    .
    .
    .
    # Create Show objects till the end_date and for each time from the 
time_list
    Show.objects.create(
        event = 1,
        date_time = datetime.strptime('2018-02-20 8:00 PM', "%Y-%m-%d %I:%M 
%p")
    )

Right now this is how I am creating Show objects:

    def create_show_by_datetime(self, request):
        event_id = request.data['event_id']
        start_date = request.data['start_date']
        end_date = request.data['end_date']
        time_list = request.data['time_list']

        format = '%Y-%m-%d'
        delta_days = datetime.strptime(end_date, format).date() - 
datetime.strptime(start_date, format).date()
        delta_days = delta_days.days + 1

        for i in range(delta_days):
            day = datetime.strptime(start_date, format) + timedelta(days=i)
            for i in range(len(time_list)):
                hrs = datetime.strptime(time_list[i], "%I:%M %p").hour
                mins = datetime.strptime(time_list[i], "%I:%M %p").minute
                event = Event.objects.get(id=event_id)
                dt = day + timedelta(hours=hrs, minutes=mins)
                show = Show.objects.create(
                    event=event,
                    date_time=dt
                )

But I am really hoping there's much more elegant way than how I am doing. I 
am using python 3, django 2 and django rest frameowork. Could you please 
help me?

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 [email protected].
To post to this group, send email to [email protected].
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/813ca752-f1e1-47d7-9ebb-a8bdb7e2ed59%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to