跳转至

Edit Hook Function

Overview

The edit hook function allows you to customize the edit operation logic in your model admin class. By overriding the handle_edit method, you can:

  • Add custom business logic before and after saving
  • Modify data before saving
  • Update additional fields
  • Implement custom validation logic

Basic Usage

Override the handle_edit method in your model admin class:

class YourModelAdmin(ModelAdmin):
    async def handle_edit(self, request: Request,
                 object_id: str, data: dict) -> tuple[bool, str]:
    """
    Handle edit operation hook method
    Args:
        request: Request object
        object_id: Record ID
        data: Edit data
    Returns:
        tuple[bool, str]: (success status, message)
    """
        try:
            # Get original object
            obj = await self.get_object(object_id)
            if not obj:
                return False, "Record not found"
            # Process form data using process_form_data method
            processed_data = await self.process_form_data(data)
            # Update object
            for field, value in processed_data.items():
                setattr(obj, field, value)
            # You can do other processing here, like writing logs or setting other database field values
            # Example: setattr(obj, "finish_time", datetime.now())
            await obj.save()
            return True, "Update successful"
        except Exception as e:
            print(f"Edit processing error: {str(e)}")
            return False, f"Update failed: {str(e)}"

Parameter Description

  • request: Robyn's Request object, containing all information about the current request
  • object_id: ID of the record to be edited
  • data: Dictionary containing edit data, where keys are field names and values are field values

Return Value

The method should return a tuple containing:

  • bool: Indicates whether the operation was successful
  • str: Operation result message