save()缺少1个必需的位置参数:使用Django Signal Dispatcher时出现“ self”错误

时间:2019-06-21 16:43:01

标签: python django django-models django-signals

models.py

class es_event(models.Model):
    #some attributes
    reg_applicants = models.FilePathField(path=None)
    ev_admin =  models.ForeignKey('es_user',related_name='events',on_delete=None)
    slug = models.SlugField(max_length=250)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.ev_name)
        return super(es_event, self).save(*args, **kwargs)

    def get_absolute_url(self):
            return reverse('event_detail', kwargs={'id': self.id, 'slug': self.slug })

views.py

class CreateEventView(LoginRequiredMixin,CreateView):
    login_url = "/login/"
    model = es_event
    fields = ['ev_name','ev_venue','ev_description','ev_date','registrant_name','registrant_age','registrant_phone','registrant_email','registrant_institution']
    def form_valid(self, form):
        form.instance.ev_admin = self.request.user.es_user
        return super(CreateEventView, self).form_valid(form)

现在将新行添加到es_event中。我想为 reg_applicants 字段设置路径(包括文件名)

例如:如果 es_event.id 为5,则文件名应为 Registered_5.csv

为此,我在 models.py

中创建了此信号
@receiver(post_save, sender=es_event)
def set_csv_path(sender, **kwargs):
    rel_path = "reg_csv/Registered_{}.csv".format(sender.id)
    path = os.path.join(settings.MEDIA_ROOT,rel_path)
    sender.reg_applicants = path
    sender.save()# error statement

这给了我这个错误

save() missing 1 required positional argument: 'self'

我认为信号分配器函数 set_csv_path()有问题。我不知道那是什么

1 个答案:

答案 0 :(得分:1)

您使用了错误的参数。 Excel.Application excelApp = new Excel.Application(); if (excelApp != null) { Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(@"C:\test.xls", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets[1]; Excel.Range excelRange = excelWorksheet.UsedRange; int rowCount = excelRange.Rows.Count; int colCount = excelRange.Columns.Count; for (int i = 1; i <= rowCount; i++) { for (int j = 1; j <= colCount; j++) { Excel.Range range = (excelWorksheet.Cells[i, 1] as Excel.Range); string cellValue = range.Value.ToString(); //do anything } } excelWorkbook.Close(); excelApp.Quit(); 是信号所连接的类别。您还需要接受实例参数。

sender