请问如何使用post_export信号更新以下导出的公司模型。 我不想导出之前未导出的项目。所以最好的方法是使用信号。
但是,如果我在下面代码末尾的信号中打印模型,我会得到一个类型类,例如<class 'app.models.Company'>
我知道通过保存模型我做错了但却找不到我的方式。
也许我不理解找到here的django-import-export
库的文档。
models.py
class Company(models.Model):
class Meta:
verbose_name_plural = "Companies"
company_name = models.CharField(max_length=254, blank=True)
website = models.URLField(max_length=254, unique=True)
address = models.CharField(max_length=254, blank=True, null=True)
imported = models.BooleanField(default=False)
exported = models.BooleanField(default=False)
user = models.ForeignKey(User)
def __str__(self):
if self.company_name:
return self.company_name
return self.domain
admin.py
from django.dispatch import receiver
from import_export.signals import post_import, post_export
class CompanyResource(resources.ModelResource):
class Meta:
model = Company
fields = ('website', 'user', 'country', 'source', 'industry')
@admin.register(Company)
class CompanyAmin(ImportExportModelAdmin):
resource_class = CompanyResource
list_display = ('domain', 'website', 'exported', 'added_on')
list_filter = ('user', 'country', 'imported', 'exported', 'added_on')
@receiver(post_export, dispatch_uid='ss1')
def _post_export(model, **kwargs):
print(model)
model.exported = True
model.save() # This line when included raises Error: "save() missing 1 required positional argument: 'self'" so code breaks here
答案 0 :(得分:1)
https://docs.djangoproject.com/en/1.11/topics/signals/#connecting-receiver-functions
请注意,该函数采用sender参数和通配符 关键字参数(** kwargs);所有信号处理人员必须采取这些 参数。
@receiver(post_export, dispatch_uid='ss1')
def _post_export(sender, model, **kwargs):
model.exported = True
model.save()
更新: django-import-export不会在信号
中发送导出的查询集所以你可以在ModelAdmin中覆盖export_action
@admin.register(Company)
class CompanyAmin(ImportExportModelAdmin):
resource_class = CompanyResource
list_display = ('domain', 'website', 'exported', 'added_on')
list_filter = ('user', 'country', 'imported', 'exported', 'added_on')
def export_action(self, request, *args, **kwargs):
response = super().export_action(request, *args, **kwargs)
qs = self.get_export_queryset(request)
qs.update(exported=True)
return response
答案 1 :(得分:1)
АрсенийКраснов解决方案有问题,在导出模型之前不更新模型。
要导出之后,必须在resources.ModelResource
中添加以下函数angular.module('app')
.factory('httpInterceptor', ["$window", "$q", "$rootScope", "$injector", function ($window, $q, $rootScope, $injector) {
return {
request: function (config) {
// Read the value from the $rootScope. If it's true, cancel all the subsequent requests
var redirected = $rootScope.app.redirected;
console.log("request: redirected: " + redirected); // The value is always 'undefined' here
var canceller = $q.defer();
if (redirected) {
// Canceling request
canceller.resolve();
}
return config;
},
responseError: function (response) {
var statusCode = response.status;
var newLocation = response.headers('location');
if (statusCode === 420 && newLocation) { // Status code is 420. Redirect and set a flag in the $rootScope
$rootScope.app.redirected = true;
$window.location.href = newLocation;
}
return response || $q.when(response);
}
}
}])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('httpInterceptor');
}]);