django-import-export

Foreign Key in django migration using django-import-export

扶醉桌前 提交于 2019-12-22 09:39:17
问题 I'm using django-import-export to load csv files in a migration file which I understand is current best practise for Django 1.7 to load initial data. This worked fine for the first file: class Country(models.Model): ISO2 = models.CharField(max_length=2, primary_key=True) name = models.CharField(max_length=50, unique=True) and the entire migration file contents. note that ISO2 is the primary key so required the addition line import_id_fields = ['ISO2']. Code adapted from answer to this

Extend django-import-export's import form to specify fixed value for each imported row

拜拜、爱过 提交于 2019-12-20 03:13:58
问题 I am using django-import-export 1.0.1 with admin integration in Django 2.1.1. I have two models from django.db import models class Sector(models.Model): code = models.CharField(max_length=30, primary_key=True) class Location(models.Model): code = models.CharField(max_length=30, primary_key=True) sector = ForeignKey(Sector, on_delete=models.CASCADE, related_name='locations') and they can be imported/exported just fine using model resources from import_export import resources from import_export

django-import-export resource definition for foreignkey field?

烈酒焚心 提交于 2019-12-11 20:49:21
问题 Given these models: class Company(models.Model): company_name = models.CharField(max_length=50, unique=True) .... class Product(models.Model): company = models.ForeignKey(Company) product_name = models.CharField(max_length=100) ... class Inventory(models.Model): product = models.ForeignKey(Product, null=True, unique=True) ... Importing from an XLS into Inventory with company_name and product_name properly specified, that is the XLS file contains a single row specifing the company_name and

django-import-export assign current user

旧巷老猫 提交于 2019-12-11 06:55:49
问题 Trying to assign added_by user while creating an instance and want to create another model instance referring to the current instance views.py class ImportFarmersView(APIView): parser_classes = (MultiPartParser,) def post(self,request,org_slug=None,format=None,*args,**kwargs): serializer=TmpFileUploadSerializer(data=request.data) if not serializer.is_valid(): return Response(data=serializer.errors,status=status.HTTP_400_BAD_REQUEST) entries=serializer.validated_data['file'] profile_resource

Django import-export display manytomany as a name instead of ids when exporting to csv

青春壹個敷衍的年華 提交于 2019-12-11 06:26:25
问题 How do I display a manytomany column as a list of names instead of ids using Django import-export ? Currently my models and resources.py looks like this and returns a csv with a list of ids for the country column: models.py class Country(models.Model): name = models.CharField(max_length=50, null=False, blank=False, unique=True) def __str__(self): return self.name class Species(models.Model): name = models.CharField(max_length=50, null=False, blank=False) country = models.ManyToManyField

Is there a way to manage the column/cell widths when exporting to Excel with django-import-export?

孤街醉人 提交于 2019-12-11 05:56:41
问题 I don't see anything about it in the docs for django-import-export. I would like to know if there is a way to change the width of columns in the exported Excel spreadsheet, so that the column becomes wide enough to fit all data? (Right now it is splitting a long piece of data between two narrow columns.) Perhaps something in the resource customization? 来源: https://stackoverflow.com/questions/38639271/is-there-a-way-to-manage-the-column-cell-widths-when-exporting-to-excel-with-dja

How to do field validation in django-import-export

☆樱花仙子☆ 提交于 2019-12-11 04:24:24
问题 Following is my model: class Product(models.Model): product_title = models.CharField(max_length=100, null=False, verbose_name='Product title') product_description = models.TextField(max_length=250, verbose_name='Product description') product_qty = models.IntegerField(verbose_name='Quantity') product_mrp = models.FloatField(verbose_name='Maximum retail price') product_offer_price = models.FloatField(verbose_name='Selling price') I wanted to have a validation for product_offer_price field

Django-import-export customization with related fields

陌路散爱 提交于 2019-12-11 04:14:26
问题 I need to update my table every time a new value of "sku" is entered (not to create a new entry), but it does have to happen only if the "client" selected is the same. If the "client" is different, then the model should add a new object with the same "sku", but with different "clients". One StackOverflow user gave me the solution: class ProductList(models.Model): id_new = models.IntegerField(primary_key=True) sku = models.CharField(primary_key=False, max_length=200) client = models.ForeignKey

Django Import-Export Import Duplicate key value violates Error

≡放荡痞女 提交于 2019-12-10 16:15:19
问题 I'm working on a project with Django 1.10, Python 3.6 and PostgreSQL as the database backend, in which I have a model called 'Article" and I need to import data from CSV files. I have imported my first CSV file successfully with the following fields: id, link & category It's ID fields starts from 1 to 1960 then in next file, I have started ID field from 1961 to onward but it shows the following error: Line number: 1961 - duplicate key value violates unique constraint "article_article_pkey"

django-import-export can not get it working

て烟熏妆下的殇ゞ 提交于 2019-12-10 15:56:00
问题 Can not get this to do anything, installed, added to apps, followed the docs. nothing. here's my admin.py: from import_export import resources from import_export.admin import ImportExportModelAdmin class EmailGroupResource(resources.ModelResource): class Meta: model = EmailGroup class EmailGroupAdmin(DjangoObjectActions, ImportExportModelAdmin, admin.ModelAdmin): resource_class = EmailGroupResource .. rest of admin admin.site.register(EmailGroup, EmailGroupAdmin) Maybe i have a conflict?