当覆盖ModelAdmin.save_model()
时,我希望能够在对象的新值与旧值之间运行一些计算。有什么方法可以在更改之前获得包含所有先前数据的“旧对象”吗?
例如,如果我有Object
obj.name
=“foo”,我通过Django管理应用更新为obj.name
=“bar”,保存以下内容后代码应该打印出来:
from django.contrib import admin
class ObjectAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
old_object = self.get_old_object()
print(old_object.name) # Should print out "foo"
print(obj.name) # Should print out "bar"
答案 0 :(得分:1)
所以你可以通过这样的数据库查找来获取对象
public class cameraf extends AppCompatActivity{
private Preview mPreview;
private Camera mCamera;
private static final String TAG = "Myact";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cameraf);
mCamera = getCameraInstance();
mPreview = new Preview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
Log.v(TAG, "will now take picture");
mCamera.takePicture(null, null, mPicture);
return true;
}
});
preview.addView(mPreview);
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.v(TAG, "Getting output media file");
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.v(TAG, "Error creating output file");
return;}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.v(TAG, e.getMessage());
} catch (IOException e) {
Log.v(TAG, e.getMessage());}
}
};
public static final int MEDIA_TYPE_IMAGE = 1;
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;}
return mediaFile;
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
}
return c; // returns null if camera is unavailable
}
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public Preview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
Camera.Parameters params = mCamera.getParameters();
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
} catch (IOException exception) {
mCamera.release();
mCamera = null;}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.startPreview();
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}
如果您需要处理它不存在的情况,您可以
old_object = self.model.objects.get(id=obj.id)
此外,try:
old_object = self.model.objects.get(id=obj.id)
except self.model.DoesNotExist:
...
只是设置为ModelAdmin中的模型类,因此您可以将其替换为模型类