I have two models: Project and Activity.
When registering a project, one or more associated activities can be added.
How can I import projects from xlsx, which include at least one activity?. I'm using the third party library django-import-export
I configure the project resource to export all the activities of each project in one cell separated by /, but I need the opposite, import all the activities of each project. I think that I must first save the Project for obtain the id, next extract the info from cell and finally save each activity associated, but I don't know how to do that.
My simplified models are:
class Project(models.Model):
reg_date= models.DateField(
default=date.today)
name = models.CharField(
max_length=100,
unique=True)
def __str__(self):
return self.name
class Activity(models.Model):
schedule= models.ForeignKey(
Project,
on_delete=models.CASCADE)
date = models.DateField()
description = models.TextField(max_length=1000)
class Meta:
unique_together = ('schedule', 'date', 'description')
class ProjectResource(resources.ModelResource):
activities = Field()
class Meta:
model = Project
import_id_fields = ['name']
exclude = ('id',)
skip_unchanged = True
report_skipped = True
fields = ('reg_date',
'name',
'activities')
def dehydrate_activities(self, obj):
if obj.id:
return "/".join([
'({0} - {1})'.format(activity.date, activity.description) for activity in obj.projectactivity_set.all()
])
def skip_row(self, instance, original, row, import_validation_errors=None):
if original.name:
return True
return False
An example of exported file is:
| reg_date | name | activities |
|---|---|---|
| 2023-01-10 | Project 1 | 2023-01-12-This is the first activity/2023-01-14-This is the second activity |
| 2023-01-10 | Project 2 | 2023-01-13-This is the first activity/2023-01-15-This is the second activity |