Re: Bulk db insert with a file through admin form

52 views
Skip to first unread message

Avraham Serour

unread,
Dec 4, 2012, 8:24:34 AM12/4/12
to django...@googlegroups.com
I also have a case where I would need to bulk add items from a file, the client receives an excel file with products (up to a couple of hundreds).
I still haven't implemented nothing yet as the feature doesn't have priority, in any case I thought adding a file upload option on the admin, or maybe a bulk add option in manage would make more sense.

in any case I don't think it would be the case of changing your model as you don't need to store this info (the file name).

in any case I believe you would need to add something custom, maybe someone has some kind of template code for bulk adding from file, I believe this could be fairly common.

any thoughts? please update on the path you took.

best luck
avraham


On Tue, Dec 4, 2012 at 5:32 AM, MNG1138 <ma...@adperk.com> wrote:
Say I've got a model like this:

class Product(models.Model):
name = models.CharField(max_length=200)

class ProductItem(models.Model):
     product = models.ForeignKey(Product)
     serialnumber = models.charField()
     sold = models.BooleanField(default=False)

ProductItem represents physical products in the store.  I have a file with thousands of serial #'s for the product.  In the product form in the admin, I'd like to upload this file, parse the serial #'s and create rows in ProductItem.  I could add a FileField to Product and create a custom storage that parses the file and creates ProductITems.  Or I could override save for the Product model.  Both of these solutions are non-optimal, as I will have a FileField in Product and db that I don't need.

Is there any way to add a 'dummy' FileField just for the form that doesn't result in a DB row?  Or is the answer to create a custom admin form?  Any good tutorials or examples for doing creating a custom admin form?

Thanks,
Mark



--
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/-/efDj8ZO4QXIJ.
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.

Chris Cogdon

unread,
Dec 4, 2012, 6:57:56 PM12/4/12
to django...@googlegroups.com
My suggestion is to not have any kind of model-linked-file. Use a standard Form that accepts a file, then as part of the POST processing, you open the file, do all the necessary reading and object creation, then return a render/redirect for success. You might want to wrap the entire view in a commit_on_success decorator which will deal with the case of a mid-file error.


If you think this is going to take a LONG time to process, risking the browser timing out, then a far more complex but reliable route is:

- DO create a model for your file uploads. All the process does is accept the upload and stick it in a file.
- Create a background process (cron job, etc) that reads in any uploaded files and does the processing, then deletes or marks the file as "processed"
- On error, you can stick a error message in a "processing_errors" TextField, for example.

Downside is that the uploader will not immediately be notified of a successful processsing

YOu could decide to process "in the background" if the file exceeds a certain size.

You could also decide to record who uploaded the file, then on completion or error, email the user with the appropriate message.

MNG1138

unread,
Jan 29, 2013, 6:35:47 PM1/29/13
to django...@googlegroups.com
What I ended up doing is customizing the ModelAdmin for the Product class.  I went this route instead of adding a new form because I wanted to be able to use the admin form to search for a particular product or products, and once located, upload the serialized product file.

The admin effectively adds a file upload form to each product in the change list.  I also customized the change_list.html admin template to add a submit button to the form when a file was selected for uploading.  When that button is pressed, a view is called which parses the uploaded files and creates the ProductItem rows in db.

While researching, I found a useful tool for easily adding pages to the django admin: https://github.com/jsocol/django-adminplus
Reply all
Reply to author
Forward
0 new messages