Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
create() in Django gives me ValueError "Field 'width' expected a number but got 'NA'

I am trying to insert several rows from .csv file into SQLite in Django. I don't want to be using import_data(), because I wanted to have a more granular control for each insertion. My model is something like this:

class Box(models.Model):
    name = models.CharField(max_length=30)
    color = models.CharField(max_length=30)
    size = LengthField(blank=True, null=True, default=None, decimal_places=2,validators=(MinValueValidator(0, field_type='Length'),MaxValueValidator(100, field_type='Length'))))

Only name has a constraint to be unique. Now, when i am running get_or_create, and have a csv row that has a blank for size, i am getting an error "ValueError - Field 'size' expected a number but got 'NA'". (For the csv rows before that, everything is inserted correctly.)

I find it strange because in the model i have blank=True and null=True for size. What could i be doing wrong and how i could fix that? Thank you in advance!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Well you try to pass the string 'NA' as value for the size field, hence that does not work. In the logic you use to create model objects, you should replace it with None, so something similar to:

name = …
color = …
size = …

#  ↓ replace 'NA' with None
if size == 'NA':
    size = None

Box.objects.get_or_create(
    name=name,
    defaults={'color': color, 'size': size}
)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!