# How to bulk import users

**URL:** <https://forum.djangoproject.com/t/how-to-bulk-import-users/10296>\
**Category:** Using Django\
**Created:** [October 28, 2021, 8:17pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296 "2021-10-28T20:17:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![RuszakMiklos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ruszakmiklos/32/6218_2.png) [@RuszakMiklos](https://forum.djangoproject.com/u/RuszakMiklos)\
**Post date:** [October 28, 2021, 8:17pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/1 "2021-10-28T20:17:41Z")

</div>

Hi! I need a little help so much…

I try to import some users from a `csv` file. I’m using the `import-export` module into the original `User` model. If I upload the file that I made it imports one user but with blank fields except `date_joined, is_staff, is_active, is_superuser` . The other fields like username, password, first and last name are empty.  
What am I doing wrong?

views.py

```auto
from tablib import Dataset
from .resources import UserResource

def simple_upload(request):
    if request.method == 'POST':
        person_resource = UserResource()
        dataset = Dataset()
        new_users = request.FILES['myfile']

        imported_data = dataset.load(new_users.read().decode(), format='csv', headers=False)
        result = person_resource.import_data(dataset, dry_run=True) # Test the data import

        if not result.has_errors():
            person_resource.import_data(dataset, dry_run=False) # Actually import now

    return render(request, 'stressz/simple_upload.html')

```

resources.py

```auto
from import_export import resources
from django.contrib.auth.models import User

class UserResource(resources.ModelResource):
    class Meta:
        model = User

```

I’m using this csv:

 ![Képernyőfotó 2021-10-28 - 15.44.26](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/4/48cb1292e425a03c6c3ec86162179efc93b40e1d.png)

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [October 29, 2021, 1:38pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/2 "2021-10-29T13:38:19Z")

</div>

Should `headers` be set to `True` when creating `imported_data`?

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [October 29, 2021, 1:42pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/3 "2021-10-29T13:42:28Z")

</div>

Also I think you should be using `imported_data` rather than `dataset` when calling `person_resource.import_data()`

---

<div class="post-metadata">

**Author:** ![RuszakMiklos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ruszakmiklos/32/6218_2.png) [@RuszakMiklos](https://forum.djangoproject.com/u/RuszakMiklos)\
**Post date:** [October 31, 2021, 8:31am UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/4 "2021-10-31T08:31:20Z")

</div>

Thank you Tim! I tried this code as you suggested if I understood it right, but it resulted to a blank user too:

```auto
imported_data = dataset.load(new_users.read().decode(), format='csv', headers=True)
result = person_resource.import_data(imported_data, dry_run=True) 

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 31, 2021, 11:27am UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/5 "2021-10-31T11:27:06Z")

</div>

It might be helpful if you posted the actual file you’re trying to read rather than the image of the data.

---

<div class="post-metadata">

**Author:** ![RuszakMiklos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ruszakmiklos/32/6218_2.png) [@RuszakMiklos](https://forum.djangoproject.com/u/RuszakMiklos)\
**Post date:** [October 31, 2021, 5:58pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/6 "2021-10-31T17:58:46Z")

</div>

Hi Ken! Thank you for the suggestion. Here is the file:

> **[new\_persons.csv](https://drive.google.com/file/d/1NJMjacEE2TVB1DzwCvjSbcyQA5ZyRmhT/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 31, 2021, 6:04pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/7 "2021-10-31T18:04:41Z")

</div>

This file doesn’t have a header line, nor do I see anything in your code assigning the columns to fields. Where are you defining how the fields in the CSV get assigned to the model fields?

---

<div class="post-metadata">

**Author:** ![RuszakMiklos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ruszakmiklos/32/6218_2.png) [@RuszakMiklos](https://forum.djangoproject.com/u/RuszakMiklos)\
**Post date:** [October 31, 2021, 9:42pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/8 "2021-10-31T21:42:33Z")

</div>

Sorry, I’m a little bit beginner. 😊 I unfortunately overwrited the file before and uploaded the overwrited one. This is the good link:

> **[new\_persons.csv](https://drive.google.com/file/d/1AEK1-3arLKXDVIZnoPJus-ftScFEAZT5/view?usp=sharing)**
>
> Google Drive file.

I followed the steps of this post where he worked with a model that he created and assigned the fields:

> **[How to Use django-import-export](https://simpleisbetterthancomplex.com/packages/2016/08/11/django-import-export.html)**
>
> As the name suggests, this is a library to handle importing and exporting data. The django-import-export librarysupports multiple formats, including xls, csv...

I am using the original User model and I did not know in this case I should define these fields and and how. 😅

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 31, 2021, 10:05pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/9 "2021-10-31T22:05:27Z")

</div>

You’ve got semicolons separating the fields and not commas.

The fields in the CSV are defined by that header row. That’s how the library can figure out which field in each row is to be assigned to a field in the model.

---

<div class="post-metadata">

**Author:** ![RuszakMiklos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ruszakmiklos/32/6218_2.png) [@RuszakMiklos](https://forum.djangoproject.com/u/RuszakMiklos)\
**Post date:** [November 1, 2021, 8:44am UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/10 "2021-11-01T08:44:07Z")

</div>

Thank you very much Ken and Tim for helping me. It was really semicolons that I don’t know why but now works. 😉

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 1, 2021, 12:15pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/11 "2021-11-01T12:15:58Z")

</div>

Nominally, the “c” in **c** sv means “comma”.

However, in your dataset.load statement, you should be able to use the `delimiter=` parameter to specify what character separates the fields - in this case `delimiter=';'` (It’s a very useful feature when you’re loading data generated from other sources.

Also, if you’re working with a source of data that doesn’t/can’t provide a header file, see [Customize resource options](https://django-import-export.readthedocs.io/en/latest/getting_started.html#customize-resource-options) for how to specify what fields are in a file and the order in which they appear.

---

<div class="post-metadata">

**Author:** ![RuszakMiklos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ruszakmiklos/32/6218_2.png) [@RuszakMiklos](https://forum.djangoproject.com/u/RuszakMiklos)\
**Post date:** [November 3, 2021, 9:01am UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/12 "2021-11-03T09:01:42Z")

</div>

Thank you very much Ken!

---

<div class="post-metadata">

**Author:** ![RuszakMiklos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ruszakmiklos/32/6218_2.png) [@RuszakMiklos](https://forum.djangoproject.com/u/RuszakMiklos)\
**Post date:** [November 5, 2021, 7:28am UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/13 "2021-11-05T07:28:14Z")

</div>

Can I have one more question?  
I made some changes on my views like the code below. I experience that if just two users in my csv, it imports the users well but if more it works not. The second problem is that every time it makes a blank user. I know this is because my `request.POST.get` script has a " " part but if it does not have this part it gives me a unique constraint failed message.

```auto
def simple_upload(request):
    if request.method == 'POST':

        username = request.POST.get('username', "")
        first_name = request.POST.get('first_name', "")
        last_name = request.POST.get('last_name', "")
        email = request.POST.get('email', "")
        user = User(username=username, first_name=first_name, last_name=last_name, email=email )
        user.save()

        person_resource = UserResource()
        dataset = Dataset()
        new_users = request.FILES['myfile']

        imported_data = dataset.load(new_users.read().decode('utf-8'), format='csv', headers=True)
        result = person_resource.import_data(imported_data, dry_run=True) # Test the data import

        if not result.has_errors():
            person_resource.import_data(dataset, dry_run=False) # Actually import now

    return render(request, 'stressz/simple_upload.html')

```

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [November 5, 2021, 2:22pm UTC](https://forum.djangoproject.com/t/how-to-bulk-import-users/10296/14 "2021-11-05T14:22:32Z")

</div>

> [@RuszakMiklos](#):
>
> I experience that if just two users in my csv, it imports the users well but if more it works not.

What happens when there are more than two users?

> [@RuszakMiklos](#):
>
> The second problem is that every time it makes a blank user. I know this is because my `request.POST.get` script has a " " part but if it does not have this part it gives me a unique constraint failed message.

When the user does not supply the username and/or email, what do you want the system to do? Once you answer that question, you can implement that as logic in an if statement to avoid creating the user in that case. You can also look into using a model form.

Another thing to keep in mind is what do you want to happen when there is a valid duplicate user being sent across? Should it error, update the user or ignore the data in the request?
