I have a function in my model that send an email on successful save. But i am struggling to figure out why print statement on signal function in the model returns empty queryset but not in the shell. Below is my code. Model code
class Booking(models.Model):
"""Database table for users booking details"""
//more code here
items = models.ManyToManyField(BookingItems)
//more code here
def send_confirmation_email(sender, instance, **kwargs):
"""Function to send email upon successfull booking creation"""
name = instance.guest_name
total = instance.final_total,
email = instance.email
order_number = instance.reservation_id
bookingId = instance.id
itemsData = instance.items.all()
booking = Booking.objects.get(id=bookingId)
bookingItems = booking.items.all()
print(bookingItems)-----------------------------------> this returns empty queryset <QuerySet []>
context = {"name": name, "items": itemsData, 'total': total,
'order_number': order_number}
message = render_to_string(
"reservations/room_reservation_success.html", context)
plain_message = strip_tags(message)
subject = f"Your booking details for {instance.hotel.name}"
mail.send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [
email], html_message=message)
post_save.connect(send_confirmation_email, sender=Booking)
Code from shell
>>> book = Booking.objects.filter(id=61)
>>> print(book[0].items.all())
<QuerySet [<BookingItems: One bedroom>, <BookingItems: Two bedroom>]>
>>>
What could be the issue here. Why is it returning empty set when there's clearly related child items? Kindly assist