本篇文章首席CTO笔记来给大家介绍有关django如何区分按键以及django 主键的相关内容,希望对大家有所帮助,一起来看看吧。
本文目录一览:
1、如何为django中的按钮绑定事件2、django 2.0外键处理3、如何给django admin中添加一个自己的按键 去做个开始停止的控制4、django 如何识别用户按了表单中的哪个按钮5、django如何捕捉键盘输入6、django中如何指定主键,比如创建ip属性,ip=IPAddressField(),括号里应该怎么设置啊。。。如何为django中的按钮绑定事件
快捷键?很简单啊如下例,在窗体pkForm中有个按钮名为tuichu,设置快捷键为CprivatevoidpkForm_KeyDown(objectsender,KeyEventArgse){if(e.KeyCode==Keys.C){tuichu_Click(null,null);}}要提醒的是先把窗体的KeyPreview设为true。当使用Ctrl+*快捷键时privatevoidpkForm_KeyDown(objectsender,KeyEventArgse){if(e.KeyCode==Keys.Fe.Control){dakai_Click(null,null);//执行单击dakai按钮的单击事件}}
django 2.0外键处理
Django2.0里model外键和一对一的on_delete参数
在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: __init__() missing 1 required positional argument: 'on_delete'
举例说明:
user=models.OneToOneField(User)
owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
参数说明:
on_delete有CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET()五个可选择的值
CASCADE:此值设置,是级联删除。
PROTECT:此值设置,是会报完整性错误。
SET_NULL:此值设置,会把外键设置为null,前提是允许为null。
SET_DEFAULT:此值设置,会把设置为外键的默认值。
SET():此值设置,会调用外面的值,可以是一个函数。
一般情况下使用CASCADE就可以了。
下面是官方文档说明:
ForeignKey accepts other arguments that define the details of how the relation works.
ForeignKey.on_delete ¶
When an object referenced by a ForeignKey is deleted, Django will emulate the behavior of the SQL constraint specified by the on_delete argument. For example, if you have a nullable ForeignKey and you want it to be set null when the referenced object is deleted:
user=models.ForeignKey(User,models.SET_NULL,blank=True,null=True,)
Deprecated since version 1.9: on_delete will become a required argument in Django 2.0. In older versions it defaults toCASCADE.
The possible values for on_delete are found in django.db.models :
CASCADE [source] ¶
Cascade deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey.
PROTECT [source] ¶
Prevent deletion of the referenced object by raising ProtectedError , a subclass of django.db.IntegrityError .
SET_NULL [source] ¶
Set the ForeignKey null; this is only possible if null isTrue.
SET_DEFAULT [source] ¶
Set the ForeignKey to its default value; a default for the ForeignKey must be set.
SET() [source] ¶
Set the ForeignKey to the value passed to SET() , or if a callable is passed in, the result of calling it. In most cases, passing a callable will be necessary to avoid executing queries at the time your models.py is imported:
fromdjango.confimportsettingsfromdjango.contrib.authimportget_user_modelfromdjango.dbimportmodelsdefget_sentinel_user():returnget_user_model().objects.get_or_create(username='deleted')[0]classMyModel(models.Model):user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.SET(get_sentinel_user),)
DO_NOTHING [source] ¶
Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQLONDELETEconstraint to the database field.
ForeignKey.limit_choices_to ¶
Sets a limit to the available choices for this field when this field is rendered using aModelFormor the admin (by default, all objects in the queryset are available to choose). Either a dictionary, a Q object, or a callable returning a dictionary or Q object can be used.
For example:
staff_member=models.ForeignKey(User,on_delete=models.CASCADE,limit_choices_to={'is_staff':True},)
causes the corresponding field on theModelFormto list onlyUsersthat haveis_staff=True. This may be helpful in the Django admin.
The callable form can be helpful, for instance, when used in conjunction with the Pythondatetimemodule to limit selections by date range. For example:
deflimit_pub_date_choices():return{'pub_date__lte':datetime.date.utcnow()}limit_choices_to=limit_pub_date_choices
Iflimit_choices_tois or returns a Qobject , which is useful for complex queries , then it will only have an effect on the choices available in the admin when the field is not listed in raw_id_fields in theModelAdminfor the model.
Note
If a callable is used forlimit_choices_to, it will be invoked every time a new form is instantiated. It may also be invoked when a model is validated, for example by management commands or the admin. The admin constructs querysets to validate its form inputs in various edge cases multiple times, so there is a possibility your callable may be invoked several times.
如何给django admin中添加一个自己的按键 去做个开始停止的控制
首先添加按钮。Django管理是jQuery让我们使用它。做静态的文件路径/ / adminfix.js:
(function($) {
$(document).ready(function($) {
$(".object-tools").append('lia href="/path/to/whatever/" class="addlink"Custom button/a/li');
});
})(django.jQuery);
在你的modeladmin admin.py:
class Media:
js = ('path/to/adminfix.js', )
django 如何识别用户按了表单中的哪个按钮
确实。
如下的表单内容:
form action="/save" method="post"
input type="hidden" name="file_name" value={{file_name}}
input name="submit" type="submit" value="save" size="" /
input name="submit" type="submit" value="cancel" size="" /
/form
我在服务器端可以通过request.post.get('submit') 为save 或者 cancel来判断用户点击了哪个变量,这样就方便多了,不需要用javascript了
【 在 pinnotherid (39.2°) 的大作中提到: 】
django如何捕捉键盘输入
后台监控软件,为了达到隐蔽监控的目的,应该满足正常运行时,不显示在任务栏上,在按Ctrl+Alt+Del出现的任务列表中也不显示,管理员可以通过热键调出隐藏的运行界面。要作到这些,必须把当前进程变为一个系统服务,并且定义全局热键。
一、把当前进程变为一个系统服务:
目的是在任务列表中把程序隐藏起来。调用API函数RegisterServiceProcess实现。
后台监控软件,为了达到隐蔽监控的目的,应该满足正常运行时,不显示在任务栏上,在按Ctrl+Alt+Del出现的任务列表中也不显示,管理员可以通过热键调出隐藏的运行界面。要作到这些,必须把当前进程变为一个系统服务,并且定义全局热键。
一、把当前进程变为一个系统服务:
目的是在任务列表中把€程序隐藏起来。调用API函数RegisterServiceProcess实现。
二、定义全局热键(本例中定义热键Ctrl+Del+R),步骤:
1、定义捕获Windows消息WM_HOTKEY的钩子函数,即:
procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
2、向Windows加入一个全局原子 Myhotkey: GlobalAddAtom('MyHotkey'),
并保留其句柄。
3、向Windows登记热键:调用API函数RegisterHotKey实现。
三、源程序:
unit Unit1;
interface
uses
Windows, Messages, Forms, Dialogs, Classes, Controls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{热键标识ID}
id: Integer;
procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
{ Privat-Declarations}
public
{ Public-Declarations}
end;
var
Form1 : TForm1;
implementation
const RSP_SIMPLE_SERVICE=1;
function RegisterServiceProcess (dwProcessID, dwType: DWord) : DWord; stdcall; external 'KERNEL32.DLL';
{$R *.DFM}
{捕获热键消息}
procedure TForm1.WMHotKey (var Msg : TWMHotKey);
begin
if msg.HotKey = id then
ShowMessage('Ctrl+Alt+R键被按下!');
form1.Visible :=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
Const
{ALT、CTRL和R键的虚拟键值}
MOD_ALT = 1;
MOD_CONTROL = 2;
VK_R = 82;
begin
{首先判断程序是否已经运行}
if GlobalFindAtom('MyHotkey') = 0 then
begin
{注册全局热键Ctrl + Alt + R}
id:=GlobalAddAtom('MyHotkey');
RegisterHotKey(handle,id,MOD_CONTROL+MOD_Alt,VK_R);
end
else
halt;
end;
{把当前进程变为一个系统服务,从而在任务列表中把程序隐藏起来}
procedure TForm1.Button1Click(Sender: TObject);
begin
RegisterServiceProcess(GetCurrentProcessID,RSP_SIMPLE_SERVICE);
form1.Hide;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
{退出时释放全局热键}
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnRegisterHotKey(handle,id);
GlobalDeleteAtom(id);
end;
end.
四、说明:
在后台监控软件中使用以上功能,可真正实现隐蔽运行,热键调出,便于管理员进行管理。程序在Win98,Delphi5.0中运行通过。
django中如何指定主键,比如创建ip属性,ip=IPAddressField(),括号里应该怎么设置啊。。。
Django1.10版本以上应该使用:
ip = models.GenericIPAddressField()
而以下的版本使用:
ip = models.IPAddressField()
结语:以上就是首席CTO笔记为大家整理的关于django如何区分按键的全部内容了,感谢您花时间阅读本站内容,希望对您有所帮助,更多关于django 主键、django如何区分按键的相关内容别忘了在本站进行查找喔。