Comments (14)
@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.
thx @bmihelac will try this way
from django-import-export.
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.
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.
Yup
from django-import-export.
Sublclassing and using ForeignKeyWidget should do the trick.
from django-import-export.
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.
This one also i snot working for me
name = widgets.ForeignKeyWidget('Autor',)
from django-import-export.
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.
export was easy and working fine
def render(self, value):
return value.name
struggling with def clean() for imports
from django-import-export.
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.
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.
I own you beer dude ;)
Cheers,
from django-import-export.
glad it worked :)
from django-import-export.
Related Issues (20)
- 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
- A way to make a header column required HOT 4
- Add support for PositiveBigIntegerField HOT 2
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.