简体   繁体   English

Django - 在使用 ModelForm 的 Createview 中将数据保存到非默认数据库时出现问题

[英]Django - problem with saving data in Createview with ModelForm to non-default database

I've got problem with saving data to non-default database.我在将数据保存到非默认数据库时遇到问题。

In models.py I've got:在 models.py 我有:

grid_fs_storage = GridFSStorage(collection='tab_userinquiries', base_url='mydomain.com/userinquiries/',database='mongo_instance')

class DocsUserInquiry(models.Model):
    query_pk = models.CharField(blank=False, null=False, unique=True, max_length=150, primary_key=True)  # auto - calculated
    query_file_md5 = models.CharField(blank=False, null=True, unique=False, max_length=200)  # auto - calculated
    query_file = models.FileField(upload_to='userinquiries',storage=grid_fs_storage,null=True)  # auto from form

In views.py:在 views.py 中:

class UploadInquiryFileView(CreateView,LoginRequiredMixin):
    model=DocsUserInquiry
    template_name ='new_inquiry.html'
    success_message = "You've added your new Token successfully"
    form_class = UploadInquiryFileForm
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)
        if form.is_valid():
            file_name = request.FILES['query_file'].name
            print(f'file...{file_name}')
            q_pk = random_id()
            file_in = self.request.FILES.get('query_file')
            f_md5 = calculate_md5(file_in)            
            form.instance.query_pk = q_pk
            form.instance.query_file_md5 = f_md5
            form.save()
            return HttpResponse(self.success_message)

The problem is every time when I submit form I've got问题是每次我提交表格时

Exception Type: TypeError
Exception Value:    database must be an instance of Database

I've tried added this to post method:我试过将此添加到发布方法中:

instance = form.save(commit=False)
instance.save(using='mongo_instance')

but the error is the same.但错误是一样的。

Any ideas how to resolve this issue?任何想法如何解决这个问题?

NOTE: This issue is related only with modelform or when I use custom list of fields in view.注意:此问题仅与模型形式有关,或者当我在视图中使用自定义字段列表时。 When I'm using CreateView without ModelForm but with fields = ' all ' and additionally with the logic passed to form_valid method of the view instead of post everything works fine.当我使用不带 ModelForm 但使用 fields = ' all ' 的 CreateView 并且另外将逻辑传递给视图的 form_valid 方法而不是发布时,一切正常。 Then files are added to my mongo db.然后将文件添加到我的 mongo 数据库中。

After some tests I've figured out the problem is in gridfs.经过一些测试,我发现问题出在 gridfs 中。 After making small change in FileField I got expected result.在对 FileField 进行小改动后,我得到了预期的结果。

I had to delete upload_to and storage from this field in models.py.我不得不从 models.py 中的这个字段中删除 upload_to 和 storage。 This saves data from my form into mongo but without the file itself.这将数据从我的表单保存到 mongo 但没有文件本身。 To do that I had to add in post method of my CreateView connection to mongo via MongoClient and save file in such separate way.为此,我必须通过 MongoClient 添加我的 CreateView 连接到 mongo 的 post 方法,并以这种单独的方式保存文件。

Completely don't know what's the issue with gridfs storage pointed directly in the model's FileField.完全不知道直接指向模型的 FileField 的 gridfs 存储有什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM