Reputation: 33
My python csv import script creates an extra empty entry in the database model every time I load data. I have tried to work around various ways but I still get one empty entry plus the actual data am loading from the csv.Here is what my script is like. Please your help will be of great achievment.
class CsvUploadView(LoginRequiredMixin, FormView):
form_class = CsvAttendeeForm
template_name = 'attendee/upload_attendee_csv.html'
success_url = reverse_lazy('home')
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
file = request.FILES.get('file')
if form.is_valid():
if file:
reader = csv.reader(file, delimiter=',')
next(reader)
attendee_instance = Attendee()
Attendee.objects.bulk_create(
[Attendee(firstname=row[0],
surname=row[1],
email=row[2],
)
for row in reader])
attendee_instance.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
Upvotes: 2
Views: 92
Reputation: 3178
Your problem is with this (cropped) section:
attendee_instance = Attendee()
Attendee.objects.bulk_create(
[Attendee(firstname=row[0],
surname=row[1],
email=row[2],
)
attendee_instance.save()
With the first attendee_instance, you're creating a blank Attendee
object. You later save this blank object with attendee_instance.save()
.
The line in the middle - Attendee.objects.bulk_create...
- you're passing in a single item list to bulk_create, so it creates the object (with data) there. You don't need both.
What you want is probably:
Attendee.objects.create(firstname=row[0],
surname=row[1],
email=row[2],
)
You don't need the bulk_create
unless you're creating more than one object, you can just use straight-up create
. Similarly, you don't need to create the object by attendee_instance=Attendee()
and then manually change the attributes, when you can do it all in one go as described above.
It's worth noting the caveats on the limitations of bulk create, particularly if you're not using Postgres, and also if you get to the point of using post_save
and pre_save
signals. The create method does trigger those save signals, so just make sure that you use the most appropriate one for your use case
Upvotes: 1
Reputation: 82078
It looks like you are initializing and saving an empty instance in addition to the bulk_create
# This line shouldn't be needed
attendee_instance = Attendee()
Attendee.objects.bulk_create(
[Attendee(firstname=row[0],
surname=row[1],
email=row[2],
)
for row in reader])
# Nor should this line.
attendee_instance.save()
Upvotes: 1