Giter VIP home page Giter VIP logo

Comments (11)

bmihelac avatar bmihelac commented on May 15, 2024

It should be possible, please see import_data method workflow documentation.

from django-import-export.

 avatar commented on May 15, 2024

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.

bmihelac avatar bmihelac commented on May 15, 2024

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.

 avatar commented on May 15, 2024

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.

bmihelac avatar bmihelac commented on May 15, 2024

No, you have to subclass ModelResource, and override there import_obj. If you still need help please post minimised example.

from django-import-export.

 avatar commented on May 15, 2024

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.

bmihelac avatar bmihelac commented on May 15, 2024

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.

 avatar commented on May 15, 2024

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.

bmihelac avatar bmihelac commented on May 15, 2024

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.

 avatar commented on May 15, 2024

Genius, it works like a charm, you just saved me a lot of tinkering 👍

from django-import-export.

bmihelac avatar bmihelac commented on May 15, 2024

glad it works for you

from django-import-export.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.