Home > front end >  Django - How to handle the UNIQUE constraint when import a excel fields
Django - How to handle the UNIQUE constraint when import a excel fields

Time:06-16

I was did a small project to import file excel and show data it. I'm using django-import-export lib

In models.py I had student_id(unique =true)

class Student(models.Model):
    school = models.CharField(max_length=100)
    district = models.CharField(max_length=100)
    student_id = models.CharField(max_length=50, unique= True, error_messages={'unique':"This email has already been registered."})
    stu_class = models.CharField(max_length=30)
    name = models.CharField(max_length=100)
    date = models.CharField(max_length=200)
    sex = models.CharField(max_length=20)
    place_of_birth = models.CharField(max_length=100)
    ethnic = models.CharField(max_length=50)
    location = models.CharField(max_length=255)
    phone_number = models.IntegerField(blank=True)
    total_grade_1 = models.PositiveSmallIntegerField(blank=True)
    total_grade_2 = models.PositiveSmallIntegerField(blank=True)
    total_grade_3 = models.PositiveSmallIntegerField(blank=True)
    total_grade_4 = models.PositiveSmallIntegerField(blank=True)
    total_grade_5 = models.PositiveSmallIntegerField(blank=True)
    total_5_years = models.PositiveSmallIntegerField(blank=True)
    plus = models.PositiveSmallIntegerField(blank=True, null= True, default=0)
    total_all = models.PositiveSmallIntegerField(blank=True, null=True)
    decripsion = models.CharField(max_length=255)

def __str__(self):
    return self.name

How I show errors_message when I import another file excel had same id_student in data. I was try to take the id_student to compare with the data import in but seen like I do it worng. views.py

def simple_upload(request):
if request.method == "POST":
    student_resource = StudentResource()
    dataset = Dataset()
    new_student = request.FILES['myfile']

    if not new_student.name.endswith('xlsx'):
        messages.error(request, 'You Must Import Excel File!')
        return redirect("upload")
    else:
        messages.success(request, 'Import Success!')

    imported_data = dataset.load(new_student.read(), format='xlsx')
    for data in imported_data:
        value = Student(
            data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],
            data[11],data[12],data[13],data[14],data[15],data[16],data[17],data[18],data[19],data[20],
        )
        # if Student.student_id == Student(data[3]):
        #     messages.error(request, 'You Must Import Excel File!')
        #     return redirect("upload")
        # else:
        value.save()
        

    #return redirect('student_list')
return render(request,'import.html')

Can some one help me.Link github if you want more detail: https://github.com/NVDungg/imexcel.git Thank you

CodePudding user response:

You can use exists() here:

     if Student.objects.filter(student_id=data[3]).exists():
         messages.error(request, 'You Must Import Excel File!')
         return redirect("upload")
     else:
        value.save()
  • Related