Import CSV to create multiple Models - Model Manager or in View?

248 views
Skip to first unread message

Victor Hooi

unread,
Oct 3, 2011, 9:42:36 AM10/3/11
to django...@googlegroups.com
heya,

I'm coding up a Django form which will let the user upload a CSV file, then create and save multiple Model instances for each row in the CSV file.

At the moment, I'm trying to decide where to put the code that parses the CSV file and creates/saves the models.

I don't think it'd be an instance method on the Model itself, since that's for working on individual instances, and you'd need to pass a valid instance to the method.

Would it go in a custom Model Manager? Or should I have this logic in the View itself?

Also, I need to figure out a good way of passing ValidationErrors from the model.save() and passing them back to the view to display the user.

What would be a good way of hooking that up?

Cheers,
Victor

Daniel Roseman

unread,
Oct 3, 2011, 10:01:53 AM10/3/11
to django...@googlegroups.com
You could put it in the Manager, certainly - MyModel.objects.from_csv(filename) would be fine.

An alternative is to use a classmethod on the model, since that doesn't suffer from the drawbacks of the instance method:

    class MyModel(models.Model):
        @classmethod
        def from_csv(cls, filename):
            ...
            for row in csv:
                obj = cls(...)
                obj.save()

--
DR.

Shawn Milochik

unread,
Oct 3, 2011, 10:03:45 AM10/3/11
to django...@googlegroups.com
A clean way would be to read in the CSV with csv.DictReader, and
ensure that the key names match your field names.

Then use a ModelForm for your model, and pass in each dict as the 'data' field.

This lets you re-use all the hard work already done in the ModelForm,
because the is_valid() method will validate your input, and the save()
method will create a new instance.

Victor Hooi

unread,
Oct 3, 2011, 10:30:24 AM10/3/11
to django...@googlegroups.com
heya,

@Shawn: Hmm, that looks like quite an interesting approach - so the ModelForm would handle the validation logic? =) Me likes! Haha.

I can parse the CSV and get the field names to match up (or just do a simple transform to get them to match).

However, how do I then pass this information into a ModelForm? Also, the CSV file is for creating multiple models - one model per CSV line - how will that work with a single ModelForm? Do I just call it repeatedly?

And how will I then capture the ValidationError and get them back in my form?

Cheers,
Victor

Shawn Milochik

unread,
Oct 3, 2011, 12:47:10 PM10/3/11
to django...@googlegroups.com
On Mon, Oct 3, 2011 at 10:30 AM, Victor Hooi <victo...@gmail.com> wrote:
> heya,
> @Shawn: Hmm, that looks like quite an interesting approach - so the
> ModelForm would handle the validation logic? =) Me likes! Haha.
> I can parse the CSV and get the field names to match up (or just do a simple
> transform to get them to match).
> However, how do I then pass this information into a ModelForm? Also, the CSV

You pass the dictionary in as the data kwarg -- the same place you'd
usually pass request.POST.

> file is for creating multiple models - one model per CSV line - how will
> that work with a single ModelForm? Do I just call it repeatedly?

Sure, in a loop

> And how will I then capture the ValidationError and get them back in my
> form?

If form.is_valid() is False, you can return form.errors.as_text() to your user.

> Cheers,
> Victor
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/wT8cDUDW62IJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

--
Shawn Milochik

CTO
Greenphire

484-334-2752
sh...@milochik.com

Reply all
Reply to author
Forward
0 new messages