@admin.register(Tours) # Register `ToursAdmin` as Admin model
class ToursAdmin(admin.ModelAdmin):
exclude = ('created_at',)
list_display = ('country',)
ordering = ('created_at',)
inlines = (HotelsInline,)
def response_add(self, request, obj, post_url_continue=None):
print(obj.pk)
print(obj.country)
print(obj.hotels.all())
return super().response_add(request, obj, post_url_continue)
def _send_mail_to_admin(self, is_new_object=False):
if is_new_object:
tour = Tours.objects.get(id=self.id)
hotels = ToursHotels.objects.filter(tour_id=tour.id).order_by('cost')
message = 'Тур ID ' + tour.id + '\n'
message += 'Страна: ' + tour.country_name + ' Город: ' + tour.country_name + '\n'
message += 'Отели: \n'
for hotel in hotels:
message += hotel.name + ' ' + hotel.star + ' ' + hotel.cost + ' руб. \n'
send_mail(
'From Admin',
message,
'no-reply@example.com',
['admin@example.com'],
fail_silently=False,
)
def save(self, *args, **kwargs):
# Define checker of new object
is_new_object = self.pk is None
# Tours model save
super(Tours, self).save(*args, **kwargs)
# Send mail
self._send_mail_to_admin(is_new_object)
def save(self, *args, **kwargs):
# Tours model save
super(Tours, self).save(*args, **kwargs)
# Define checker of new object
is_new_object = self.pk is None
# Send mail
self._send_mail_to_admin(is_new_object)
Если логика сохранения требует редактировать несколько моделей, то деаем все в форме