How can I send data via copy paste operation in Django Views or model?

I have such a very difficult problem. Which is very specific and not easy to enter data.

I am thinking how to send data to the database or, as an option, just send data to the code in the internal Django code for processing in Views.

Is it possible, for example, to have the ability to send data not one by one in the singular, to fill in the input field.

Fill in the input field not only one in the singular. For example, as the most common case of data processing in office or other applications - the copy / paste operation.

So in this frequent popular operation - for example, we want to copy several elements in an Excel table and try to insert them somehow send to the code in the internal Django code for processing in Views.

Is it possible to implement something similar?

So as not to manually enter data, but somehow have the ability to do this in a semi-automatic form - by copying several cells with data and pasting them into some kind of form or something similar?

How can I send data via copy paste operation in Django Views or model?

from django import forms
  
class InputForm(forms.Form):
  
    first_name = forms.CharField(max_length = 200)
    last_name = forms.CharField(max_length = 200)
    roll_number = forms.IntegerField(
                     help_text = "Enter 6 digit roll number"
                     )
    password = forms.CharField(widget = forms.PasswordInput())

from django.shortcuts import render
from .forms import InputForm
  
# Create your views here.
def home_view(request):
    context ={}
    context['form']= InputForm()
    return render(request, "home.html", context)

<form action = "" method = "post">
    {% csrf_token %}
    <table>
        {{ form.as_table }}
    </table>
    <input type="submit" value="Submit">
</form>

So if I understand your query correctly, you want to:

  • Copy and Paste data from Excel;

  • Put this into a form;

  • Pull this data into your database on submission not as a single entity, but as multiple entities.

Assuming the above is correct, I will also assume that:

  • Each row of data in your Excel corresponds to a row (single object) in your database's table, and

  • Each column in your Excel data corresponds to a field in that table (you don't mention the name of your model, but it has first_name and last_name fields, so I'll refer to this as your Person model).

Working on this basis, you should be able to achieve your aim of importing in bulk via Copy + Paste by relying on the following two facts:

A Copy + Paste of Excel data typically treats spaces between columns as Tabs (\t) and spaces between rows as Newlines (\n).

So theoretically you can achieve your aim by Copy + Pasting into a single textarea form widget, and then manipulating this data in either the InputForm or your home_view; first split your data into rows by splitting on Newlines, and then splitting each row into fields by splitting on Tabs. You should then be able to save to your model correctly.

An example implementation is below:

# I assume you have a model like this in models.py
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=250)
    last_name = models.CharField(max_length=250)
    roll_number = models.PositiveIntegerField(unique=True)
    password = models.CharField(max_length=250)

# Rewrite your InputForm in forms.py as follows:
from django import forms
from .models import Person

class InputForm(forms.Form):
    bulk_data = forms.CharField(
        widget=forms.Textarea(
            attrs={'rows': 10, 'cols': 80} # Adjust size as needed
        ),
        help_text=(
            "Paste data from Excel in following format: "
            "FirstName<TAB>LastName<TAB>RollNumber<TAB>Password<NEWLINE>."
        )
    )

# Handle inside your views.py as follows:
from django.shortcuts import render
from .forms import InputForm
from .models import Person

def home_view(request):
    if request.method == 'POST':
        form = InputForm(request.POST)
        if form.is_valid():
            raw_data = form.cleaned_data['bulk_data']
            rows = raw_data.strip().split('\n')
            for line in rows:
                fields = line.strip().split('\t')
                if len(fields) == 4:
                    first_name, last_name, roll_number, password = fields
                    roll_number = int(roll_number) # Convert roll num to int
                    Person.objects.create(
                        first_name=first_name,
                        last_name=last_name,
                        roll_number=roll_number,
                        password=password
                    )
            return render(request, 'home.html', {'form': InputForm(), 'success': True})
    else:
        form = InputForm()
    return render(request, 'home.html', {'form': form})

Then in your home.html template, have the following (notice it is not a table, nor rendered as one):

<form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>

Let me know if this works for you - I tried it with some dummy data and a few debugging statements and it worked perfectly (see below screenshot):

enter image description here

Вернуться на верх