Django Custom Admin Form bulk save implmentation
<p>I am trying to implement a CSV Import in Django Admin and save bulk data corresponding to the CSV file's rows.</p>

<p>This is my Admin class:</p>

<pre><code>class EmployeeAdmin(admin.ModelAdmin):
    list_display = ('user', 'company', 'department', 'designation', 'is_hod', 'is_director')
    search_fields = ['user__email', 'user__first_name', 'user__last_name']
    form = EmployeeForm
</code></pre>

<p>This is my Form class:</p>

<pre><code>class EmployeeForm(forms.ModelForm):
    company = forms.ModelChoiceField(queryset=Companies.objects.all())
    file_to_import = forms.FileField()

    class Meta:
        model = Employee
        fields = ("company", "file_to_import")

    def save(self, commit=True, *args, **kwargs):
        try:
            company = self.cleaned_data['company']
            records = csv.reader(self.cleaned_data['file_to_import'])
            for line in records:
                # Get CSV Data.

                # Create new employee.
                employee = CreateEmployee(...)

        except Exception as e:
            raise forms.ValidationError('Something went wrong.')
</code></pre>

<p>My Employee class is:</p>

<pre><code>class Employee(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    company = models.ForeignKey(Companies)
    department = models.ForeignKey(Departments)
    mobile = models.CharField(max_length=16, default="0", blank=True)
    gender = models.CharField(max_length=1, default="m", choices=GENDERS)
    image = models.ImageField(upload_to=getImageUploadPath, null=True, blank=True)
    designation = models.CharField(max_length=64)
    is_hod = models.BooleanField(default=False)
    is_director = models.BooleanField(default=False)
</code></pre>

<p>When I upload my file and click <code>save</code>, it shows me this error:</p>

<p><code>'NoneType' object has no attribute 'save'</code></p>

<p>with exception location at:</p>

<p><code>/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py in save_model, line 1045</code></p>

<p><strong>EDIT</strong> I understand I need to put a call to <code>super.save</code>, but I am unable to figure out where to put the call, because the <a href="https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#the-save-method" rel="nofollow">doc</a> says that the save method saves and returns the instance. But in my case, there is no single instance that the superclass can save and return. Wham am I missing here?</p>

<p>TIA.</p>

<p>You should just add the <code>super().save()</code> to the the end of the function:</p>

<pre><code>def save(self, *args, commit=True, **kwargs):
    try:
        company = self.cleaned_data['company']
        records = csv.reader(self.cleaned_data['file_to_import'])
        for line in records:
            # Get CSV Data.

            # Create new employee.
            employee = CreateEmployee(...)
        super().save(*args, **kwargs)
    except Exception as e:
        raise forms.ValidationError('Something went wrong.')
</code></pre>

python django csv