测试带有获取参数的自定义模型功能功能
我正在尝试为以下ModelAdmin函数编写测试:
def response_add(self, request, obj, post_url_continue=None):
""" Redirects to the public profile details page after creation if return=true """
if request.GET.get('return'):
return redirect(reverse('public_profile_details', kwargs={'id': obj.id}))
else:
return redirect('/admin/public/publicprofile/')
目的是员工通过“编辑”链接从网页访问管理面板(而不是直接从管理员面板中),我想将其返回到该页面保存后。我通过附加?return = tue tum admin链接来做到这一点。
但是,我很难编写测试来介绍这一点。
这是我获得的最接近的:
def test_admin_functions(self):
self.login_privileged_user()
self.GET = {'return': True}
public_profile_model_admin = admin.PublicProfileAdmin(model=models.PublicProfile, admin_site=AdminSite())
response = self.client.get('/admin/public/publicprofile/1/change/?return=True')
public_profile_model_admin.response_add(self, response, models.PublicProfile.objects.get(id=1))
但是,这返回以下错误:
AttributeError: 'TemplateResponse' object has no attribute 'id'
我可以稍微操纵它(从self.client.get.get到self.client.post),并得到其他各种错误,例如httpresponsponse错误,httpresponsedirect错误,等等。但是所有这些都有相同的“对象没有属性'id'”的错误。
我知道我可以使用硒来做到这一点,但是我试图避免尽可能少的测试。
I'm trying to write a test for the following modelAdmin function:
def response_add(self, request, obj, post_url_continue=None):
""" Redirects to the public profile details page after creation if return=true """
if request.GET.get('return'):
return redirect(reverse('public_profile_details', kwargs={'id': obj.id}))
else:
return redirect('/admin/public/publicprofile/')
The purpose of this is when staff access the admin panel from the webpage via an "Edit" link (rather than from the admin panel directly), I want to return them to that page once the object is saved. I do this by attaching ?return=True to the admin link.
However, I'm having trouble writing a test to cover this.
This is the closest I've gotten:
def test_admin_functions(self):
self.login_privileged_user()
self.GET = {'return': True}
public_profile_model_admin = admin.PublicProfileAdmin(model=models.PublicProfile, admin_site=AdminSite())
response = self.client.get('/admin/public/publicprofile/1/change/?return=True')
public_profile_model_admin.response_add(self, response, models.PublicProfile.objects.get(id=1))
However, this returns the following error:
AttributeError: 'TemplateResponse' object has no attribute 'id'
I can manipulate it slightly (changing from self.client.get to self.client.post) and get various other errors such as HttpResponse error, HttpResponseRedirect error, etc. But all of them have the same error of "object has no attribute 'id'".
I know I could use Selenium to do this, but I'm trying to avoid that for my smaller tests as much as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,使用以下内容来弄清楚它:
差异是访问响应的.wsgi_request
Nevermind, figured it out using the following:
The difference was in accessing the .wsgi_request of the response