3

This has been asked several times- but none of the solutions worked for me.

The code below works (in that there are no errors) but it does not see anything to import new data to the foreign key class. It will only import data if it already exists in the foreign key.

Does that make sense?

Models.py (snippet)

...
class Store(models.Model):

    store_name = models.CharField(max_length=30)
    def __unicode__(self):
        return self.store_name
    #etc

class Product(models.Model):

    Store = models.ForeignKey(Store)
    Category = models.ForeignKey(Category)
    first_name = models.CharField(max_length=30)
    second_name = models.CharField(max_length=30)
...

Admin.py

admin.site.register(Category)
admin.site.register(Store)

class ProductResource(resources.ModelResource):

     store_name = fields.Field(column_name='store_name', attribute='Store',
                       widget=ForeignKeyWidget(Store, 'store_name'))

    def __unicode__(self):
        return self.store_name.name

    class Meta:
        model = Product
        fields = ('id', 'first_name', 'second_name','store_name')
        export_order = ('id', 'second_name', 'first_name')
        skip_unchanged = False
        report_skipped = False
        widgets = {
                'published': {'format': '%d.%m.%Y'},
                }


class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource
    list_display = ('first_name', 'second_name')

admin.site.register(Product, ProductAdmin)

2 Answers 2

6

Try

 store_name = fields.Field(column_name='store_name', attribute='Store',
                           widget=ForeignKeyWidget(Store, 'store_name'))

I suggest you name model field uncapitalized

It's hard to understand what you are really asking.

If what you are saying is that "Want to create foreign key objects and fill the foreign object's attribute with the excel data"

I think you need to do first create the foreign object and relate it to the other model which means, you would need to override or define a function in your ModelResource class and have it called from your admin.

1
  • Thank you. I didn't read the error properly- the issue is that I want to IMPORT new fields into the foreign key. It only lets me import values (to foreign fields) that already exist. How can I say to import values if data doesn't exist?
    – Ycon
    Aug 26, 2015 at 5:54
1

They have a widget for this- http://django-import-export.readthedocs.org/en/latest/api_widgets.html. I do not understand how to use it tho.

1
  • Yes- I tried this method too. Would really appreciate any direction on it. Here's what I've got to (and I updated the original post). codeclass ProductResource(resources.ModelResource): store_name = fields.Field( widget=widgets.ForeignKeyWidget(Store, 'store_name')) def __unicode__(self): return self.store_name.name class Meta: model = Product fields = ('id', 'first_name', 'second_name','store_name') .....
    – Ycon
    Aug 26, 2015 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.