Django: Download the file and read its contents to populate the model?

I am new to Django and would like to know that the Django path for adding items to the database is not by entering each field from an html form (as it is done by default), but loading a single file (for example, a json file) to be used to populate the database?

So, let the model have only three fields: name, description, quantity. And I have a text file (myFile.txt) with the inscription "myTitle: myDesc".

I only want a FileField that will accept a text file so that I can load myFile.txt, and the title and description will be read from this file. And at the same time, the quantity will be requested β€œnormally” in the text input, as it would be by default (only the title and description are read from the file).

Of course, a file check will be performed to accept / deny the downloaded file. The problem I am facing is that if I add a FileField to the model, the file will be saved in local storage. I want the contents of the download file to be read, used to create a record in the model, and then deleted.

Even the administrator should not be able to manually add an element that introduces the title and description in the form of HTML, but only by downloading the file. Can someone help me in Django-way?

+8
django django-admin file-upload
source share
4 answers

I found another way to populate the model before saving it.

Instead of using pre_save or using two different forms, if we use admin.ModelAdmin, we can simply override the save_model () method:

def save_model(self, request, obj, form, change): obj.user = request.user # populate the model obj.save() # post actions if needed 
+1
source share

You can create two forms:

  • A form based on django.forms.Form that is used to receive a file on request
  • The model form that is used to validate model fields and create a model object

Then you can call the second form from the first, for example:

 class MyModelForm(ModelForm): class Meta: model = MyModel class FileUploadForm(forms.Form): file = forms.FileField() def clean_file(self): data = self.cleaned_data["file"] # read and parse the file, create a Python dictionary `data_dict` from it form = MyModelForm(data_dict) if form.is_valid(): # we don't want to put the object to the database on this step self.instance = form.save(commit=False) else: # You can use more specific error message here raise forms.ValidationError(u"The file contains invalid data.") return data def save(self): # We are not overriding the `save` method here because `form.Form` does not have it. # We just add it for convenience. instance = getattr(self, "instance", None) if instance: instance.save() return instance def my_view(request): form = FileUploadForm(request.POST, request.FILES) if form.is_valid(): form.save() else: # display errors 
+8
source share

You can use the form wizard to accomplish such tasks. The basic idea is to create two forms; one with FileField and the other with a heading, description quantity fields.

First, the user views the form with FileField . After the user uploads the file and sends the request, you can display another form with the initial values ​​read from the file (you can also delete the file at this point).

Regarding the functionality of the administrator, you can read about how to integrate the form wizard with the administrator here

+2
source share

To do this, you need to write some code. Each FileField has an associated File object . You can read the contents of this file object as if you were dealing with files in Python.

There are, of course, different places where you could do this. You can overwrite the method for saving forms / models that contains FileField. If you have a model, you can use pre_save / post_save .

0
source share

All Articles