我无法解释的一些行为:
我正在关注djangobook.com教程并连接了一个简单的遗留mysql数据库
我已经定义了这样的模型:
class Sentences(models.Model):
sentenceid = models.IntegerField(primary_key=True)
sentence = models.TextField(blank=True)
class Meta:
db_table = u'sentences'
def __unicode__(self):
return unicode(self.sentence)
class Character(models.Model):
charid = models.IntegerField(primary_key=True)
symbol = models.TextField(blank=True)
class Meta:
db_table = u'characters'
def __unicode__(self):
return unicode(self.symbol)
非常标准的东西。
我正在测试通过shell访问它。尽管在(我认为是)中完全相同的方式定义了这些字符,但这些字符的表现却很奇妙,但句子却很奇怪。
>>> from myapp.models import Character
>>> c = Character.objects.filter(charid=70)
>>> c
[<Character: β>]
这很完美,但是:
>>> from myapp.models import Sentences
>>> s = Sentences.objects.get(sentenceid=25)
>>> s
<Sentences: Sentences object>
>>> print s
Sentences object
>>> print s.sentence
The quick brown fox jumps over the lazy dog.
为什么不在第一种情况下返回句子?为什么我必须使用s.sentence呢?
太长了吗?
我只是偶然发现了s.sentence的作品。
答案 0 :(得分:0)
更新:你还有另外一个问题。在这里,使用您的型号代码:
class Sentences(models.Model):
sentenceid = models.IntegerField(primary_key=True)
sentence = models.TextField(blank=True)
class Meta:
db_table = u'sentences'
def __unicode__(self):
return unicode(self.sentence)
__unicode__
效果很好:
In [1]: from testapp.models import Sentences
In [2]: Sentences(sentence='foo').save()
DEBUG (0.000) INSERT INTO "sentences" ("sentenceid", "sentence") VALUES (None, foo); args=(None, 'foo')
In [3]: Sentences.objects.all()
DEBUG (0.000) SELECT "sentences"."sentenceid", "sentences"."sentence" FROM "sentences" LIMIT 21; args=()
Out[3]: [<Sentences: foo>]
另一个较长句子的尝试:
In [1]: from testapp.models import Sentences
In [2]: Sentences(sentence='The quick brown fox jumps over the lazy dog. ').save()
DEBUG (0.000) INSERT INTO "sentences" ("sentenceid", "sentence") VALUES (None, The quick brown fox jumps over the lazy dog. ); args=(None, 'The quick brown fox jumps over the lazy dog. ')
In [3]: Sentences.objects.all()
DEBUG (0.000) SELECT "sentences"."sentenceid", "sentences"."sentence" FROM "sentences" LIMIT 21; args=()
Out[3]: [<Sentences: foo>, <Sentences: The quick brown fox jumps over the lazy dog. >]
所以请检查:
添加 unicode 定义
Sentences.__module__
返回正确的app.class(此处:testapp.models)
Sentences.__unicode__
已定义
有效:
In [9]: Sentences.__unicode__
Out[9]: <unbound method Sentences.__unicode__>
失败了:
In [1]: from testapp.models import Sentences
In [2]: Sentences.__unicode__
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/jpic/testproject/<ipython-input-2-dcbda9dfb929> in <module>()
----> 1 Sentences.__unicode__
AttributeError: type object 'Sentences' has no attribute '__unicode__'
END UPDATE
filter()会返回一个QuerySet,就像一个对象列表。因此,值[<Character: β>]
周围的括号:
>>> Character.objects.filter(charid=70)
[<Character: β>]
如果您使用get(),则直接获取角色实例
>>> Character.objects.get(charid=70)
<Character: β>
字符模型有一个'符号'TextField属性,您可以访问该属性,例如:
>>> Character.objects.get(charid=70).symbol
u'β'
您理解 Sentences.objects.get()返回单个Sentences对象是完全正常的:
>>> Sentences.objects.get(sentenceid=25)
<Sentences: Sentences object>
现在,您的 Sentences模型有一个“句子”TextField属性,您可以这样访问:
>>> Sentences.objects.get(sentenceid=25).sentence
The quick brown fox jumps over the lazy dog.
一切正常,行为也是一致的。