Suggestion on how to make import and export of products using csv from admin panel?

526 views
Skip to first unread message

Sathish Anton

unread,
Apr 29, 2016, 2:06:33 PM4/29/16
to Mezzanine Users
Hi,
Please provide Suggestion on how to make import and export of products using csv from admin panel?

Thanks and Regards,
Sathish Anton. A

Ken Bolton

unread,
Apr 29, 2016, 2:15:18 PM4/29/16
to mezzanine-users
Hi Sathish,

What have you tried so far? There is plenty of documentation out there
on working with Django and CSV, and plenty more on Django's admin
interface.

If you are not using Python 3, I can caution you to use the unicodecsv
module: https://pypi.python.org/pypi/unicodecsv.

hth,
ken
> --
> You received this message because you are subscribed to the Google Groups
> "Mezzanine Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mezzanine-use...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Stephen McDonald

unread,
Apr 29, 2016, 11:07:45 PM4/29/16
to Mezzanine Users
There's a potentially outdated CSV import script here:


That should get you 90% of the way

--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Stephen McDonald
http://jupo.org

Eduardo Rivas

unread,
Apr 29, 2016, 11:10:59 PM4/29/16
to mezzani...@googlegroups.com
Adding to the suggestions, I've used django-import-export in the past
with satisfactory results. It does admin integration out of the box and
let's you pick different import/export formats (Excel, csv, json, etc).
I've only used it to export records; can't speak for the importing part.
The docs will guide you all the way through.

https://django-import-export.readthedocs.io/en/latest/

Sathish Anton

unread,
Apr 30, 2016, 10:48:36 AM4/30/16
to Mezzanine Users
Hi,
The import seems to be an issue with both Product_db command and django-import-export.
Can some one explain exact shell  commands that would be required to add a product to
   1. shop category
   2. shop/subcategory
from python manage.py shell or 
some other sugestion on utils available.

Thanks and Regards,
Sathish Anton. A

Eduardo Rivas

unread,
Apr 30, 2016, 12:00:55 PM4/30/16
to mezzani...@googlegroups.com

What have you tried so far? What errors are you getting?

Sathish Anton

unread,
Apr 30, 2016, 12:37:01 PM4/30/16
to Mezzanine Users
Hi,
W.r.t Product_db command i tried the following with single django pony sample data loaded with createdb.
1. python manage.py product_db --export cur.csv
2. Deleted the single django product using admin panel manualy so that there wont be any issues with indexing or sku.
3. python manage.py product_db --import cur.csv  
    
The errors observed are,
  1. Additional empty shop category gets created as the field for subcategory is blank in csv.
  2. There seems to issues with image path. Probaly i have to adust the following parametes for this in product_db.py
      LOCAL_IMAGE_DIR, 
      SITE_MEDIA_IMAGE_DIR
      PRODUCT_IMAGE_DIR
or probaly some documnetation on what values should be set should help.


I also tried django-import-export util by following the example mentioned in read docs
1. I was able to see admin panel having import and export options.,
2. Export of data was succesfull.
3. Import was successful was sucessfull but database was corrupted.

Probaly i need an understanding of mandatory fields required to add a product to make this work.


Thanks,
Sathish Anton. A

Sathish Anton

unread,
Apr 30, 2016, 3:05:40 PM4/30/16
to Mezzanine Users
Hi,
This is what i did with django-import-export
   1. pip install django-import-export
  2.# add to settings.py
INSTALLED_APPS = (
    ...
    'import_export',
)
3. Modified lib/python2.7/site-packages/cartridge/shop and added following code
from import_export import resources
from import_export.admin import ImportExportModelAdmin
from import_export.admin import ImportExportMixin, ImportMixin, ExportActionModelAdmin
class CategoryAdmin(PageAdmin):
!class CategoryAdmin(ExportActionModelAdmin, PageAdmin):
class ProductVariationAdmin(admin.TabularInline):
!class ProductVariationAdmin(ImportMixin, admin.TabularInline):
class ProductImageAdmin(TabularDynamicInlineAdmin):
!class ProductImageAdmin(ImportMixin, TabularDynamicInlineAdmin):
+class ProductResource(resources.ModelResource):

+    class Meta:
+        model = Product

class ProductAdmin(ImportExportModelAdmin, DisplayableAdmin):
!class ProductAdmin(ImportExportModelAdmin, DisplayableAdmin):
      +resource_class = ProductResource

1. python manage.py runserver
2. http://127.0.0.1:8000/admin/shop/product/ 
    Import export options are available in admin.
3. First do an export of csv file say current.csv
4. Delete Django pony sample product.
5. Do import of same current.csv 
The Product gets added without any errors.
However the errors observed are
1. The variation fields does not gets filled up.
2. Multiple images are not loaded.
3. Unit price and sales price are not updated.
I am not sure how the product variation are to be handled.
Thanks,
sathish Anton. A

Sathish Anton

unread,
May 1, 2016, 9:05:42 AM5/1/16
to Mezzanine Users
Hi,
The management command product_db works with some changes made.
1. mkdir "/tmp/orig/product" where LOCAL_IMG_DIR="/tmp/orig"
2. Copy all your imagefiles to "/tmp/orig/product"
3. Make small changes as mentioned in the new product_db.py attached with this post.

Repeat test:

1. python manage.py product_db --export sample.csv
2. Delete the Django pony product manualy from the admin panel.
3. Import the same file
   python manage.py product_db --import sample.csv

Thanks and regards,
Sathish Anton. A
product_db.py.diff
product_db.py.orig
product_db.py

Sathish Anton

unread,
May 2, 2016, 11:49:03 AM5/2/16
to Mezzanine Users

Hi,
Please let me know  if
You have any idea on lncluding the inline models
inlines = (ProductImageAdmin, ProductVariationAdmin)
 in django-import-export.

Thanks,
Sathish Anton. A

On Saturday, April 30, 2016 at 9:30:55 PM UTC+5:30, Eduardo Rivas wrote:

Eduardo Rivas

unread,
May 2, 2016, 11:52:48 AM5/2/16
to mezzani...@googlegroups.com
You will need to create separate imports for separate models. At that
point it might be better to create your own admin view to import several
models at a time, calling the individual model importer in the right order.

Sathish Anton

unread,
May 3, 2016, 12:05:23 PM5/3/16
to Mezzanine Users
Hi,
Here is a method to override django-import-export functions with product_db import export function.
1. pip install django-import-export
2. Add to# settings.py
INSTALLED_APPS = (
    ...
    'import_export',
)
3. Add image files to /tmp/orig/product
4. Make changes to the /home/sathish/impexp/lib/python2.7/site-packages/cartridge/shop/admin.py as in the attached file.
5. python manage.py runserver
6. Enter admin panel
7. click on products
8. You will find import and export buttons in admin panel
9. Do export of file using csv format as csv format is only supported now for importing.
10. save the downloaded file.
11. Delete the single django pony product for testing purpose.
12. Do import of same csv file.
13. Review wont produce any output as import function is overridden by product_db import.
14. confirm submit. and products will be added as done by product_db.
Thanks and Regards,
Sathish Anton. A


On Friday, April 29, 2016 at 11:36:33 PM UTC+5:30, Sathish Anton wrote:
admin.py.orig
admin.py.diff
admin.py

Eduardo Rivas

unread,
May 4, 2016, 1:16:21 PM5/4/16
to mezzani...@googlegroups.com
Glad to see you figured it out.

The only suggestion I have is that you should avoid editing Cartridge
source code, and instead register your own customized admin class in
your project. That way future Cartridge updates won't break your code
(as easily) and you know all your customizations live in your project.
Reply all
Reply to author
Forward
0 new messages