Здравствуйте на основе этой библиотеки https://django-import-export.readthedocs.org/en/latest/installation.html
я делаю импорт csv, при импорте добавляются также и связи, вот проблема эта связь не добавляется в выборе категории данного города, вот мой код
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
class Location(models.Model):
name = models.CharField('russia name', max_length=100)
#ename = models.CharField('english name', max_length=100)
iata = models.CharField('iata', max_length=100)
oon = models.CharField('oon', max_length=100)
typelocation = models.CharField('typelocation', max_length=100)
ngrad = models.IntegerField(blank=True, null=True)
nmin = models.IntegerField(blank=True, null=True)
wgrad = models.IntegerField(blank=True, null=True)
wmin = models.IntegerField(blank=True, null=True)
typetime = models.CharField('typetime', max_length=100)
class Meta:
abstract = True
class City(models.Model):
name = models.CharField('russia name', max_length=100)
def __unicode__(self):
return self.name
class Contry(models.Model):
name = models.CharField('russia name', max_length=100)
def __unicode__(self):
return self.name
class Airport(Location):
city = models.ForeignKey(City, related_name='city', blank=True, null=True)
contry = models.ForeignKey(Contry, related_name='contry', blank=True, null=True)
def __unicode__(self):
return self.name
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from import_export import fields,widgets
from import_export.widgets import ForeignKeyWidget
from import_export.admin import ImportExportModelAdmin
from import_export.admin import ImportExportActionModelAdmin
from import_export.admin import ImportMixin, ExportMixin
from django.contrib.admin import DateFieldListFilter
from import_export import resources, fields
from import_export import resources
from .models import City, Airport, Contry
class StoreWidget(widgets.ForeignKeyWidget):
def clean(self, value):
return self.model.objects.get_or_create(name = value)[0]
class Cityresources(resources.ModelResource):
city = fields.Field(column_name='city', attribute='City', widget=StoreWidget(City, 'name'))
class Meta:
model = Airport
fields = ('city','name')
def get_instance(self, instance_loader, row):
# Returning False prevents us from looking in the
# database for rows that already exist
return False
class AirportAdmin(ImportExportModelAdmin,admin.ModelAdmin):
resource_class = Cityresources
fieldsets = [
('name', {'fields':['name']}),
('city', {'fields':['city']}),
('contry', {'fields':['contry']}),
('iata', {'fields':['iata']}),
('oon', {'fields':['oon']}),
('ngrad', {'fields':['ngrad']}),
('nmin', {'fields':['nmin']}),
('wgrad', {'fields':['wgrad']}),
('wmin', {'fields':['wmin']}),
('typetime', {'fields':['typetime']}),
]
admin.site.register(Airport, AirportAdmin)
admin.site.register(Contry)
admin.site.register(City)
я импортирую файл с таким содержанием
city,name
Москва ,Домодедово
ну потом приходится редактировать и добавлять нужный город, если я уберу операции с добавлением связи FK, то при импортировании документа пройдет все нормально, но конечно с тем учетом что нужный город будет присутствовать в базе, не могу понять как решить этот вопрос, основной затык был реализовать FK, а тут получается на мелочи застрял((