我正在探索如何使用Django,我在我的Django应用程序中创建了两个模型。
public void DetectKeyboard()
{
AttemptKeyboardDetection:
try
{
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero), GetCurrentThreadId(), true);
IntPtr focus = GetFocus();
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero), GetCurrentThreadId(), false);
if (GetClassName(focus, this.StringBuilder, this.StringBuilder.Capacity))
{
ActiveProcessFocus = this.StringBuilder.ToString();
Debug.WriteLine("Active Process: " + ActiveProcessFocus);
if ((PreviousProcessFocus != ActiveProcessFocus) && !ActiveProcessFocus.Contains("KeyboardOpener"))
{
Debug.WriteLine("Checking if editing...");
if ((((ActiveProcessFocus == "Edit") || (ActiveProcessFocus == "SearchPane")) || (ActiveProcessFocus.Contains("RichEdit") || ActiveProcessFocus.Contains("SearchEdit"))) || ((ActiveProcessFocus.Contains("TextfieldEdit") || ActiveProcessFocus.Contains("Afx:00400000")) || (((ActiveProcessFocus == "_WwG") || (ActiveProcessFocus == "Scintilla")) || (ActiveProcessFocus == "SPEAD0C4"))))
{
Debug.WriteLine("Editing! Opening keyboard...");
try
{
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
catch
{
}
}
else
{
try
{
SendMessage(FindWindow("IPTip_Main_Window", null), 0x112, (IntPtr) 0xf060, IntPtr.Zero);
foreach (Process process in Process.GetProcessesByName("osk"))
{
process.Kill();
}
}
catch
{
}
}
PreviousProcessFocus = ActiveProcessFocus;
}
}
Thread.Sleep(250);
goto AttemptKeyboardDetection;
}
catch
{
goto AttemptKeyboardDetection;
}
}
[DllImport("user32.dll")]
private static extern IntPtr AttachThreadInput(IntPtr AttachId, IntPtr AttachToId, bool AttachStatus);
[DllImport("user32.dll")]
private static extern bool GetClassName(IntPtr hWnd, StringBuilder ClassName, int ClassMax);
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentThreadId();
[DllImport("user32.dll")]
private static extern IntPtr GetFocus();
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ThreadId);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
第一个模型是人,第二个模型是技能。现在关系如何,每个人都会有很多技能。
现在我可以用数据更新数据库,网站的管理部分也可以正常工作。
在Django Shell上,我尝试运行命令:
from django.db import models
#first model
class Person(models.Model):
name = models.CharField(max_length=40)
email = models.CharField(max_length=100)
title = models.CharField(max_length=100)
image = models.CharField(max_length=200)
def __str__(self):
return self.name
#second model
class Skill(models.Model):
person = models.ForeignKey(Person)
skill = models.CharField(max_length=60)
years = models.CharField(max_length=40)
def __str__(self):
return self.skill, self.person
我得到的是以下错误:
Skill.object.all()
或者如果我尝试命令:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\query.py", line 235, in __repr__
return '<QuerySet %r>' % data
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\base.py", line 572, in __repr__
u = six.text_type(self)
TypeError: __str__ returned non-string (type tuple)
我得到:
Skill.objects.get(pk=1)
但是,如果我运行如下命令:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\base.py", line 572, in __repr__
u = six.text_type(self)
TypeError: __str__ returned non-string (type tuple)
我得到了具有“Photoshop”技能的人的名字。
我想知道我在这里做错了什么;也许我不应该用这种方式用外键查询表?或许我做错了什么。
嗯,最后我想查询的是,我想找到具有给定名称或主键的人的所有技能。
答案 0 :(得分:1)
__str__
应返回str
。所以改变这样的事情
return self.skill, self.person
到
return "%s-%s" %(self.skill, self.person.name)
答案 1 :(得分:1)
您的__str__
方法返回一个元组(self.skill,self.person),它必须返回那些对象的str表示。为了实现这一目标,请更改:
return self.skill, self.person
到
return "{}, {}".format(self.skill, self.person)