我是django的新手,我在测试自定义操作时遇到了问题(例如,操作= [' mark_as_read']),这些操作位于app_model_changelist的下拉列表中,它是&#39 ; s与标准"删除所选"相同的下拉列表。自定义操作在管理视图中工作,但我不知道如何在我的模拟请求中调用它,我知道我需要发布数据但是如何说我想要" mark_as_read"要对我发布的数据采取什么行动?
我想反转更改列表网址并发布查询集,以便" mark_as_read"动作功能将处理我发布的数据。
change_url = urlresolvers.reverse('admin:app_model_changelist')
response = client.post(change_url, <QuerySet>)
答案 0 :(得分:23)
只需使用操作名称传递参数action
。
response = client.post(change_url, {'action': 'mark_as_read', ...})
选中的项目将作为_selected_action
参数传递。所以代码将是这样的:
fixtures = [MyModel.objects.create(read=False),
MyModel.objects.create(read=True)]
should_be_untouched = MyModel.objects.create(read=False)
#note the unicode() call below
data = {'action': 'mark_as_read',
'_selected_action': [unicode(f.pk) for f in fixtures]}
response = client.post(change_url, data)
答案 1 :(得分:3)
这就是我的所作所为:
data = {'action': 'mark_as_read', '_selected_action': Node.objects.filter(...).values_list('pk', flat=True)}
response = self.client.post(reverse(change_url), data, follow=True)
self.assertContains(response, "blah blah...")
self.assertEqual(Node.objects.filter(field_to_check=..., pk__in=data['_selected_action']).count(), 0)
与接受的答案相比,有一些注释:
values_list
代替列表理解来获取ID。follow=True
,因为预计成功的帖子会导致重定向答案 2 :(得分:1)
这是您使用登录名和所有内容(完整的测试用例)进行操作的方式:
from django.test import TestCase
from django.urls import reverse
from content_app.models import Content
class ContentModelAdminTests(TestCase):
def setUp(self):
# Create some object to perform the action on
self.content = Content.objects.create(titles='{"main": "test tile", "seo": "test seo"}')
# Create auth user for views using api request factory
self.username = 'content_tester'
self.password = 'goldenstandard'
self.user = User.objects.create_superuser(self.username, 'test@example.com', self.password)
def shortDescription(self):
return None
def test_actions1(self):
"""
Testing export_as_json action
App is content_app, model is content
modify as per your app/model
"""
data = {'action': 'export_as_json',
'_selected_action': [self.content._id, ]}
change_url = reverse('admin:content_app_content_changelist')
self.client.login(username=self.username, password=self.password)
response = self.client.post(change_url, data)
self.client.logout()
self.assertEqual(response.status_code, 200)
只需进行修改即可使用模型和自定义操作并运行测试。
更新:如果您获得302,则可能需要在follow=True
中使用self.client.post()
。
答案 3 :(得分:0)
请注意,即使 POST 成功,您仍然需要测试您的操作是否成功执行了预期的操作。
这是直接从 Admin 类测试操作的另一种方法:
Future<void> uploadVideo() async {
List<String> filesPath = [];
await Future.forEach(imgSource, (AssetEntity element) async {
File file = await element.file;
filesPath.add(file.path);
});
await Workmanager().registerOneOffTask(
"1",
uploadFileTask,
inputData: <String, dynamic>{
'filesPath': filesPath,
},
constraints: Constraints(
networkType: NetworkType.connected,
requiresBatteryNotLow: true,
),
backoffPolicy: BackoffPolicy.exponential,
existingWorkPolicy: ExistingWorkPolicy.keep,
);
}
// Here is callbackDispatcher function
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
switch (task) {
case uploadFileTask:
try {
List<dynamic> dynamicType = inputData["filesPath"];
List<String> filesPath =
dynamicType.map((e) => e.toString()).toList();
await Firebase.initializeApp();
int counter = 1;
List<String> downloadUrlLinks = [];
await Future.forEach(filesPath, (String filePath) async {
File file = File(filePath);
String fullPath = getRandomName(file.path);
String storagePath = "test/$fullPath";
print("Full path HM" + fullPath);
String downloadUrl = await CloudService.uploadFileWithProgressBar(
file: file,
filePath: storagePath,
maxLength: filesPath.length,
uploadedLength: counter,
);
downloadUrlLinks.add(downloadUrl);
counter++;
});
await NotificationService.finishedNotification(
title: 'Uploading files finished');
print("download link: " + downloadUrlLinks.toString());
downloadUrlLinks = [];
} catch (e) {
print("uploading error" + e.toString());
}
break;
}
return Future.value(true);
});
}