Hi,
I have tried a number of ways to do this but I cannot get Foreign Keys to 
work with non-pk values. I keep getting a int type when trying to import a 
charfield, 'MotorMakeName' even though this is the type of field upon which 
the foreign key is connected. I've included the additional ids I used to 
force the imports to work, however this involves manipulating data manually 
on the datasets which I would like to avoid. Any help much approeciatted.

Please see model below;

class MotorMakes(models.Model):
    MotorMakeName = models.CharField(max_length=50, unique=True, 
default=False)
    
    def __str__(self):
        return self.MotorMakeName or ''
    
    def __unicode__(self):
        return u'%s' % (self.MotorMakeName) or ''

class MotorModelsV2(models.Model):
    MotorMakeName =  
models.ForeignKey(MotorMakes,on_delete=models.CASCADE,to_field='MotorMakeName', 
default=False)
    MotorModelName = models.CharField(max_length=50, default=False,)
    #Mkid = models.ForeignKey(MotorMakes,on_delete=models.CASCADE, 
default=False) ** I add this field in to force the FK to work, but it 
should work otherwise
    
    def __str__(self):
        return self.MotorModelName or ''

    def __unicode__(self):
        return u'%s' % (self.MotorModelName) or ''

class MotorDetail(models.Model):
    MotorMakeName2 = models.ForeignKey(MotorMakes,on_delete=models.CASCADE, 
default=False, null=True)
    MotorModelName = 
models.ForeignKey(MotorModelsV2,on_delete=models.CASCADE, default=False, 
null=True)
    title =  models.CharField(max_length=25, default=False,)
    fuel =  models.CharField(max_length=5, default=False,)
    body = models.CharField(max_length=5, default=False,)
    engine = models.CharField(max_length=5, default=False,)
    Mkid = models.CharField(max_length=5, default=False,)
    Mlid = models.CharField(max_length=5, default=False,)


Admin.py

import import_export
from import_export.admin import ImportExportModelAdmin
from import_export.widgets import ForeignKeyWidget
from django.contrib import admin
from .models import MotorDetail
from .models import MotorMakes
from .models import MotorModelsV2
#from .models import Users
from import_export import resources



################ Include impirt/export functions for each Model 
###################

class MotorMakesFileAdmin(ImportExportModelAdmin):
    pass

class MotorModelsV2FileAdmin(ImportExportModelAdmin):
    pass

class MotorDetailFileAdmin(ImportExportModelAdmin):
    pass


class MotorMakeResource(resources.ModelResource):
    class Meta:
        model = MotorMakes
        exclude = ('is_active',)

class MotorModelResource(resources.ModelResource):
    class Meta:
        model = MotorModelsV2
        exclude = ('is_active',)

class MotorModelResource(resources.ModelResource):
    motormakename = import_export.fields.Field(
        column_name='MotorMakeName1',
        attribute='MotorMakeName1',
        widget=ForeignKeyWidget(MotorMakes, 'MotorMakeName'))

    class Meta:
        fields = ('MotorMakeName1',)

##################################################################
################ Admin Site Registration #########################
admin.site.register(MotorModelsV2, MotorModelsV2FileAdmin)
admin.site.register(MotorMakes, MotorMakesFileAdmin)
admin.site.register(MotorDetail, MotorDetailFileAdmin)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fccc781b-165c-4b8c-b2b9-8a7d3953a914o%40googlegroups.com.

Reply via email to