1

How can I set null or empty value of my array list inorder to insert id autoincrement , I already tried 'None' but it returns error. Can anyone help me?

error tuple indices must be integers or slices, not NoneType

views.py

def upload_excel_file(request):
    if request.method =='POST':
        person_resource = PersonResource()
        dataset = Dataset()
        new_person = request.FILES['myfile']

        if not new_person.name.endswith('xlsx'):
            messages.info(request,'wrong format')
            return render (request,'dashboard.html')
        imported_data = dataset.load(new_person.read(),format="xlsx")
        for data in imported_data:
            value = Person(
                data[None],  // It should Empty value 
                data[1],
                data[2],
                data[3],
                data[4],
                data[5]
            )
            value.save()
    return render(request,'dashboard.html')

model.py

class Person(models.Model):
   person_id = models.AutoField(primary_key=True)
   covid_id = models.CharField(max_length=50)
   firstname = models.CharField(max_length=50)
   middle = models.CharField(max_length=50)
   lastname   = models.CharField(max_length=50,blank=True)
   extension  = models.CharField(max_length=50)
2
  • show your Person model Commented Oct 1, 2020 at 14:57
  • @Andrey Maslov thank you response I've added my models above Commented Oct 1, 2020 at 15:02

3 Answers 3

1

you can just skip your auto_id_field

value = Person(
                covid_id=data[1],
                firstname=data[2],
                middle=data[3],
                lastname=data[4],
                extension=data[5]
            )
Sign up to request clarification or add additional context in comments.

Comments

0

To set null value to the model field in django. You can simply assign None value to the field. So:

def upload_excel_file(request):
    if request.method =='POST':
        person_resource = PersonResource()
        dataset = Dataset()
        new_person = request.FILES['myfile']

        if not new_person.name.endswith('xlsx'):
            messages.info(request,'wrong format')
            return render (request,'dashboard.html')
        imported_data = dataset.load(new_person.read(),format="xlsx")
        for data in imported_data:
            value = Person(
                None,  # <--  Now null value is given to the field
                data[1],
                data[2],
                data[3],
                data[4],
                data[5]
            )
            value.save()
    return render(request,'dashboard.html')

Comments

0

Please do not make use of positional arguments. It makes it harder to read to what field what value is passing. You can use iterable unpacking an positional parameters:

for __, covid, first, mid, last, ext, *__ in imported_data:
    value = Person.objects.create(
        covid_id=covid,
        firstname=first,
        middle=mid,
        lastname=last,
        extension=ext
    )

By using .create(…) [Django-doc] you already save the object to the database in the same statement.

1 Comment

Thank! you it helps me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.