Django-import-export - Export one to many relationship with ForeignKeyWidget - returns an empty Field

I am trying to use the dajngo-import-export package to export data from two tables with a one to many relationship. I have a custom ForeignKeyWidget class that overrides the get_queryset method.

The problem is that the export returns an empty field - no errors, just an empty field. I also tried just using the ForeignKeyWidget without the custom class/get_queryset - but I get the same result.

Does anyone see what I'm doing wrong here?

from import_export import resources
from import_export.fields import Field
from import_export.widgets import ForeignKeyWidget

class SlateDocResource(resources.ModelResource):

    actbreaks = Field(
        widget=ActBreaksForeignKeyWidget(ActTimecodes, "slatedoc_id"),

class Meta:
    model = SlateDoc
    fields = [

class ActBreaksForeignKeyWidget(ForeignKeyWidget):
    def get_queryset(self, value, row, *args, **kwargs):
        qs = ActTimecodes.objects.filter(
        return qs

class SlateDoc(models.Model):

    #primary Model - fields not listed here.

class ActTimecodes(models.Model):
    #Secondary model - every slatedoc can has multiple instances of ActTimecodes

    slatedoc = models.ForeignKey(
    act_number = models.IntegerField(verbose_name="Act", default=1)
    tc_in = models.CharField(max_length=11, default="00:00:00:00")
    tc_out = models.CharField(max_length=11, default="00:00:00:00")
    dur = models.CharField(max_length=11, default="00:00:00:00")

    objects = ActTimecodesQuerySet.as_manager()

    class Meta:
        ordering = ["act_number", "tc_in", "tc_out"]

#version info

"python_version": { "version": ==3.10" }

"django": { "version": "==4.1.1" },

"django-import-export": { "version": "==2.8.0"},

Here is the solution that I figured out. The answer is very simple compared to what I was attempting to do - using the ForeignKey was totally unnecessary. 

class SlateDocResource(resources.ModelResource):

    actbreaks = Field(column_name="Act Breaks")

    def dehydrate_actbreaks(self, slatedoc):
        actbreaks = []
        count = 1
        for x in ActTimecodes.objects.filter(
            tc_in = f"{count}_in"
            tc_out = f"{count}_out"
            duration = f"{count}_dur"
            actbreak = {tc_in: x.tc_in, tc_out: x.tc_out, duration: x.dur}
            count += 1
        return actbreaks

the code above returns each actbreak as a dict in a list:

[{'tc_1_in': '01:00:00:00', 'tc_1_out': '01:13:34:00', 'act_1_dur': '00:13:34;00'}, {'tc_2_in': '01:13:36:00', 'tc_2_out': '01:19:03:00', 'act_2_dur': '00:05:26;28'}, {'tc_3_in': '01:19:05:00', 'tc_3_out': '01:26:13:00', 'act_3_dur': '00:07:08;02'}, {'tc_4_in': '01:26:15:00', 'tc_4_out': '01:31:16:00', 'act_4_dur': '00:05:01;02'}, {'tc_5_in': '01:31:18:00', 'tc_5_out': '01:37:39:00', 'act_5_dur': '00:06:21;00'}, {'tc_6_in': '01:37:41:00', 'tc_6_out': '01:44:10:00', 'act_6_dur': '00:06:29;00'}]
Back to Top