小编Bry*_*ock的帖子

如何在交互式会话中(在iPython中)向现有类添加模型方法?

我有一个基本的模型:

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    state = USStateField() 
Run Code Online (Sandbox Code Playgroud)

我启动了一个iPython会话:

$ python manage.py shell
>>> from app.models import Person
Run Code Online (Sandbox Code Playgroud)

如何在iPython会话中添加此模型方法?

>>>    def is_midwestern(self):  
...        "Returns True if this person is from the Midwest."  
...        return self.state in ('IL', 'WI', 'MI', 'IN', 'OH', 'IA', 'MO')  

>>> person = Person.objects.filter(last_name='Franklin')
>>> person.is_midwestern
True
Run Code Online (Sandbox Code Playgroud)

我希望能够测试这些模型方法,而无需将该方法添加到models.py文件,然后重新启动iPython shell会话.

我似乎做错了,因为当我在交互式会话中添加新的模型方法时,它似乎没有像在文件中定义模型方法时那样链接到类.

因此,如果我创建了上面的模型方法并尝试使用它.例如'>>> person = Person.objects.filter(last_name ='Franklin')'is_midwestern'`
>>> person.is_midwestern
'Person' object has no attribute

python django ipython

1
推荐指数
1
解决办法
859
查看次数

为什么我的应用引擎查询找不到我的实例?

我一直在玩App Engine,但我似乎误解了NDB数据存储区查询.我把抛出的错误放在查询旁边.

在交互式控制台中玩游戏:

from google.appengine.ext import ndb  

class Client(ndb.Model):  
    email =  ndb.StringProperty()  
    name = ndb.StringProperty(indexed=True)  

#instantiated client instance with the parameters below. ID is 6578378068983808  
#client = Client(email = "bryan@gmail.com", name = "Bryan Wheelock" ).put()  

client = Client.query( Client.name == 'Bryan Wheelock')  
#client = Client.query( Client.ID == 6578378068983808 ) #AttributeError: type object 'Client' has no attribute 'ID'  
#client = Client.all() #AttributeError: type object 'Client' has no attribute 'all'    
#client = Client.get_by_id(6578378068983808) #THIS WORKS returns u'Bryan Wheelock'   

pprint.pprint(client.name)  
Run Code Online (Sandbox Code Playgroud)

我所做的示例查询完全取决于App Engine文档,我做错了什么?

python google-app-engine app-engine-ndb google-cloud-datastore

1
推荐指数
1
解决办法
107
查看次数

这个正则表达式有什么问题?

我正在尝试创建一个测试来验证链接是否在网页上呈现.

我不明白我在这个断言测试中做错了什么:

self.assertRegexpMatches( response.content, r'<a href="/questions/?sort=elite" class="on" title="Staff Selected Questions">elite</a>')
Run Code Online (Sandbox Code Playgroud)

我知道标记在页面上,因为我从response.content复制了它

我试图在Python shell中使用正则表达式:

In [27]: links = """<div class="tabsA"><a href="/questions/?sort=active" title="Most recently updated questions">active</a><a href="/questions/?sort=newest" title="most recently asked questions">newest</a><a href="/questions/?sort=hottest" title="most active questions in the last 24 hours">hottest</a><a href="/questions/?sort=mostvoted" title="most voted questions">most voted</a><a href="/questions/?sort=elite" class="on" title="Staff Selected Questions">elite</a></div>"""

In [28]: re.search(r'<a href="/questions/?sort=elite" class="on" title="Staff Selected Questions">elite</a>', links)
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它也没有奏效.

如何创建正则表达式以使其有效?

python django unit-testing

0
推荐指数
1
解决办法
1625
查看次数

如何使用GAE的最终一致性确认实体是否已保存?

我正在尝试创建测试以验证我的实体是否正在保存在数据库中.当我在post函数中放置断点时,我可以看到保存记录后客户计数发生了变化.我阅读了https://cloud.google.com/appengine/docs/python/tools/localunittesting#Python_Writing_High_Replication_Datastore_tests

根据我的理解,由于最终的一致性,测试失败了,解决这个问题的方法是改变PseudoRandomHRConsistencyPolicy设置.

policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)  
Run Code Online (Sandbox Code Playgroud)

当我再次运行测试时,我得到了同样的错误.

创建这些测试我做错了什么?

> /Users/Bryan/work/GoogleAppEngine/dermalfillersecrets/main.py(137)post()  
-> customer.put()  
(Pdb) l  
134             query = Customer.query()  
135             orig_customer_count = query.count()  
136             import pdb; pdb.set_trace()  
137  ->         customer.put()  
138             import pdb; pdb.set_trace()  
139             query_params = {'leadbook_name': leadbook_name}  
140             self.redirect('/?' + urllib.urlencode(query_params))  
141       
142     config = {}  
(Pdb) orig_customer_count  
5  
(Pdb) c  
> /Users/Bryan/work/GoogleAppEngine/dermalfillersecrets/main.py(139)post()  
-> query_params = {'leadbook_name': leadbook_name}  
(Pdb) l  
134             query = Customer.query()  
135             orig_customer_count = query.count()  
136             import pdb; pdb.set_trace()  
137             customer.put()  
138             import pdb; pdb.set_trace()  
139 …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine selenium-webdriver google-cloud-datastore nose-gae

-1
推荐指数
1
解决办法
409
查看次数