Giter VIP home page Giter VIP logo

Comments (14)

bmihelac avatar bmihelac commented on May 14, 2024

@grillazz fields from related models would not be imported automatically. I advise you to read Import data workflow to see which methods you can override if you need to populate related models.

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

thx @bmihelac will try this way

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

Actally. I don't need to populate related models. I just want to use related model Authors containing few records which I added first to populate books using author name not author id for author = models.ForeginKey(Authors,)
What you think @bmihelac ?

from django-import-export.

bmihelac avatar bmihelac commented on May 14, 2024

If I understand, your Book model has FK to Author and you would like to import this FK by author name and not pk?

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

Yup

from django-import-export.

bmihelac avatar bmihelac commented on May 14, 2024

Sublclassing and using ForeignKeyWidget should do the trick.

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

Not sure that below is ok. Think it is wokring just for export :/

class BookResource(resources.ModelResource):
    name = fields.Field(widget=widgets.ForeignKeyWidget(Author,))

    class Meta:
        model = Book
        fields = ('id', 'name',)
        pass

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

This one also i snot working for me

name = widgets.ForeignKeyWidget('Autor',)

from django-import-export.

bmihelac avatar bmihelac commented on May 14, 2024

I have something like this in my mind, this is totally untested

class MyForeignKeyWidget(ForeignKeyWidget):
     def clean():
         # return author  object for author name
     def render():
         # retur author name

class BookResource(resources.ModelResource):
    author = fields.Field(column_name='author', attribute='author', widget=widgets.MyForeignKeyWidget(Author,))

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

export was easy and working fine

   def render(self, value):
        return value.name

struggling with def clean() for imports

from django-import-export.

bmihelac avatar bmihelac commented on May 14, 2024

See how it is made in ForeignKeyWidget.

https://github.com/bmihelac/django-import-export/blob/master/import_export/widgets.py#L128

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

yup working

    def clean(self, value):
        t1 = super(widgets.ForeignKeyWidget, self).clean(value)
        return self.model.objects.get(name=t1) if t1 else None

from django-import-export.

grillazz avatar grillazz commented on May 14, 2024

I own you beer dude ;)
Cheers,

from django-import-export.

bmihelac avatar bmihelac commented on May 14, 2024

glad it worked :)

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.