Wednesday, October 16, 2019

Re: Import a float field using a script

Hi again,

On 16/10/2019 13.02, 'David Turner' via Django users wrote:
> Thanks for your response
>

You're welcome.

> I am using djjango-extensions to run the script. Everything imports fine
> except the one field. The Category, Region, County and Iso's are foreign
> keys to the property.
>
> The data in the csv for the area_hectares does contain in some cases no
> data but the format for the data is:
> 158.9265
> 70
> 58.9
>
> 150
> 7200000
> 665.03
>

Since no data is represented as an empty string you need to special case
handle that so that gets converted to a NoneType instead.

Instead of simply calling float(row[6]) in your import script (which
will raise the ValueError exception on empty strings) a solution could
be to write a small wrapper function that tests if the input argument is
an empty string and returns None in that case, otherwise calls the float
type converter as usual.

That function should be trivial to write, but that's not the only
solution of course.

Thinking about, I'm not 100% sure that a nullable field in Django can be
created with a NoneType (that will then set the type to Null), but I'm
fairly certain. Someone please correct me if I'm wrong.

Kind regards,

Kasper Laudrup

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a7420722-8818-4c89-3eb7-4224c70dfc9d%40stacktrace.dk.

No comments:

Post a Comment