I am trying to filter a queryset based on dates, I keep getting a duplicate error on the for loops,How do I refactor these code to remove duplicates? I am trying to filter the events table to get ongoing events and complete events, but pycharm keeps underlining my code because of duplicated code. How do I prevent for loop code duplicates on the two functions below?
Better yet, how do I optimally refactor these two functions?
Thanks

def get_ongoing_events():
    ongoing_events = Events.objects.filter(
            Q(event_begin_datetime__lte=current_time),
            Q(event_end_datetime__gt=current_time),
        )
    for event in ongoing_events:
        event.event_status = 'ongoing'
        event.save()

        event.venue.status = 'booked'
        event.venue.save()

        reserve_data = dict()
        reserve_data[&quot;sensor_id&quot;] = event.venue.sensor_id
        reserve_data[&quot;status&quot;] = event.venue.status

        return reserve_data


def get_complete_reservation():
    &quot;&quot;&quot;
    Update reservations and sensors
    :return:
    &quot;&quot;&quot;
    completed_events = Events.objects.filter(
            Q(reservation_begin_datetime__lt=current_time),
            Q(reservation_end_datetime__lte=current_time),
        )

    for event in completed_events:
        event.reservation_status ='complete'
        event.save()

        event.venue.status = 'free'
        event.venue.save()
        reserve_data = dict()
        reserve_data[&quot;sensor_id&quot;] = event.venue.sensor_id
        reserve_data[&quot;status&quot;] = event.venue.status
        return reserve_data
```
Factoring duplication out
Whenever I have (near-)duplicate code, what I do is take a look at the repeated code. The parts that change ever so slightly are going to be controlled with arguments to functions and etc, whereas the parts that remain the same will just be left more-or-less as-is.
Having said that, here is what I see:

the queries being used change, so those are function arguments;
the attributes of the event being updated change, so those are function arguments;
the final dict being generated is the same, so we leave it the same.

From your code alone, here is a suggested modification:
def filter_and_update_events(Qs, event_changes, venue_changes):
    for event in Events.objects.filter(*Qs)
        for attr, new_value in event_changes.items():
            setattr(event, attr, new_value)
        event.save()
        for attr, new_value in venue_changes.items():
            setattr(event.venue, attr, new_value)
        event.venue.save()

        reserve_data = dict()
        reserve_data[&quot;sensor_id&quot;] = event.venue.sensor_id
        reserve_data[&quot;status&quot;] = event.venue.status
        return reserve_data

def get_ongoing_events():
    return filter_and_update_events(
        [
            Q(event_begin_datetime__lte=current_time),
            Q(event_end_datetime__gt=current_time),
        ],
        {&quot;event_status&quot;: &quot;ongoing&quot;},
        {&quot;status&quot;: &quot;booked&quot;},
    )

def get_complete_reservation():
    &quot;&quot;&quot;
    Update reservations and sensors
    :return:
    &quot;&quot;&quot;
    return filter_and_update_events(
        [
            Q(reservation_begin_datetime__lt=current_time),
            Q(reservation_end_datetime__lte=current_time),
        ],
        {&quot;reservation_status&quot;: &quot;complete&quot;},
        {&quot;status&quot;: &quot;free&quot;},
    )

Notice that I used setattr docs to set the attributes of the event and the venue.
Also notice that my new function has a bit of duplication in the loops, but that is the easiest way to deal with the fact that venue is inside event and there is no obvious way to use setattr to deal with the nesting.
If you need to expand your function to update even more things inside event, then I would also recommend creating a helper function that takes an event and a &quot;setting name&quot; and sets it, so that filter_and_update_events doesn't need to take one dictionary per object inside event.
Does this make sense?