SoFunction
Updated on 2025-03-02

Django logs log instances before Model saving

How to do certain fixed operations before saving Model in Django, such as writing a log?

Keywords: Signal

Using the Signal Dispatcher of Django's Model, the .pre_save() method is used before the event occurs.

emission

The trigger signal is all saved by the reception method in the dispatched process and name.

Signal processing is generally written in Model, for example:

import logging
from  import models
from  import pre_save
from  import receiver
 
class Order():
  # ...
 
logger = (__name__)
 
@receiver(pre_save, sender=Order)
def pre_save_handler(sender, **kwargs):
  
  # We can tease ourselves before the Order model is saved :)  ("{}".format(sender, **kwargs))
  print 'fuck universe'

This should realize the requirements in the question. Similar methods include pre_init triggering before the Model instance, post_init triggering after the instance. Similarly, pre_save and post_save.

Supplementary knowledge:Django () problem

Django references to do() and then do .save.

We cannot save it directly and submit it to the database.

EX:

After we get the information through post here.

  def post(self, request, ab=None, all_seat=None):

    # Obtain user information    date = ('date')
    time = ('time')

Then

seatform = SeatForm()
          # Submit to the database          #The is_valid() here is Django default, check whether it is correct        if seatform.is_valid():
          # Add a new information to the database here and save it          getSeat = (dateTime=date, classtime=time, status='0', user=name, number=label)
          ()

The above SeatForm() is created in the same directory as the same level.

class SeatForm():
#Note that the date and time here need to be named with the front-end  date = ()
  time = ()

In this way, we can save it to the database.

The above example of Django logging before model saving is all the content I share with you. I hope it can give you a reference and I hope you can support me more.