Comments (11)
It should be possible, please see import_data method workflow documentation.
from django-import-export.
I checked it and what seems possible is before_save_instance and import_obj not sure how to pass fields to it like this
class MyModelDataset(ModelDataset):
fields = [
'boring_field_name',
'id',
'some_other_field',
]
headers = {
'boring_field_name': 'Awesome Descriptive Column Header',
}
class Meta:
model = MyModel
from django-import-export.
import_obj
gets row
argument - you could override this method to set get relevant values and set them as instance attribute of obj
, ie: obj.my_model_field = row['some_field'] + row['other_field']
.
from django-import-export.
Ok, I've been looking for the best way to do this with the least tinkering and from import_export/admin.py line 147
result = resource.import_data(dataset, dry_run=True,
raise_errors=False)
Will it look like result = resource.import_data(dataset, dry_run=True,
raise_errors=False).my_model_field
Thanks for helping me so far.
from django-import-export.
No, you have to subclass ModelResource
, and override there import_obj
. If you still need help please post minimised example.
from django-import-export.
My excel sheet has these field: Name, Surname, Bd/Sr, Company, Wk PO Box, Tel, Fax, E-mail, Slv/Gld
And my model looks like this
class Contact(models.Model):
name = models.CharField(max_length=64, )
description = models.TextField(blank=True, null=True,)
pic = RemovableImageField(blank=True, null=True, upload_to=settings.UPLOAD_DIR,)
address = models.TextField(blank=True, null=True,)
telephone = models.CharField(max_length=256, help_text="Separate multiple telephone numbers with a '/'", blank=True, null=True,)
fax = models.CharField(max_length=256, help_text="Separate multiple fax numbers with a '/'", blank=True, null=True,)
email = models.EmailField(max_length=256, blank=True, null=True,)
website = models.URLField(max_length=256, blank=True, null=True,)
sortorder = models.IntegerField(db_index=True, blank=True, null=True,)
path = models.SlugField(max_length=64, default='',)
class Meta:
abstract = True
Need to relate them this way:
name=Name+Surname
id=Bd/Sr
sortorder=Br/Sr
telephone = Tel
level = if Slv/Gld='Sil': return 1 elif Slv/Gld='Gld': return 2 else: return 3
from django-import-export.
Something like this should give you a good start:
#resources.py
from import_export import fields, resources
class ContactResource(resources.ModelResource):
# override id it is exact mapping
id = fields.Field(column_name='Bd/Sr', attribute="id")
class Meta:
model = Contact
def import_obj(instance, row):
super(ContactResource, self).import_obj(instance, row)
instance.name = "%s %s" % (row['Name'], row['Surname'])
if row['Slv/Gld'] == 'Sil':
instance.level = 1
# and so on
#admin.py
from resources import ContactResource
class ContactAdmin(ImportExportModelAdmin):
resource_class = ContactResource
# ...
from django-import-export.
Trying it on the Book model and got this error from this setting
class BookResource(resources.ModelResource):
id = fields.Field(column_name='Bd/Sr', attribute="id")
class Meta:
model = Book
def import_obj(instance, row):
super(ContactResource, self).import_obj(instance, row)
instance.name = "%s %s" % (row['Name'], row['Surname'])
if row['Slv/Gld'] == 'SIL':
instance.price = 1
elif row['Slv/Gld'] == 'GLD':
instance.price = 2
else:
instance.price = 3
Errors
Line number: 1 - TypeError('import_obj() takes exactly 2 arguments (3 given)',)
Traceback (most recent call last):
File "../import_export/resources.py", line 271, in import_data
self.import_obj(instance, row)
TypeError: import_obj() takes exactly 2 arguments (3 given)
from django-import-export.
On Thu, Jul 25, 2013 at 2:56 PM, Samuel Muiruri [email protected]:
def import_obj(instance, row):
sorry, it should be: def import_obj(self, instance, row):
from django-import-export.
Genius, it works like a charm, you just saved me a lot of tinkering 👍
from django-import-export.
glad it works for you
from django-import-export.
Related Issues (20)
- Unknown documents in resources.py if you use a intersphinx setup
- Custom Import Form: Bug when form fields are also in Resource.import_id_field HOT 3
- Test needed to cover `collect_failed_row` option HOT 4
- 'list' object is not callable HOT 2
- ImproperlyConfigured at /admin/.. HOT 2
- Failing test in different python environments
- skip_row doesn't work correctly HOT 15
- The error message for invalid column names is misleading
- Export the excel as password protected file HOT 3
- `is_create` param should be passed into new `do_instance_save()` method
- `SimpleArrayWidget.render()` crashes if value is None
- Djangonaut Space navigator interest
- how to skip error for confirm: '"Field 'id' expected a number but got '1-variant'. " HOT 2
- Not able to export to xls with UUIDs HOT 2
- Support dynamic selection of Resource class based on request property
- Issue with adding rows with generated field HOT 6
- Remove support for django3.2
- Add type hints HOT 3
- Confirmation page displays incorrect fields
- Using a ManyToManyWidget does not allow to ignore an empty value HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-import-export.