Search will not work for Foreign Key fields with "to_field" attribute set.
dgurovich opened this issue · comments
I am building a complex app with many Foreign Key fields on a particular model that I'm reporting on. All of the filter/search features work, EXCEPT in one instance, where I have the "to_field" set to make the rest of the system work correctly. It just returns nothing found, no errors, etc...
Is this a known issue? Here is the some code.
class Architect(models.Model): """ A lookup table for CX Enterprise Architects, used mostly for reporting purposes. Associated with Initiatives and Features. """ id = models.AutoField(primary_key=True) name = models.CharField(max_length=60, verbose_name="Lead Architect", null=False, unique=True, blank=False, db_index=True)
class Initiative(models.Model): id = models.AutoField(primary_key=True)
null=True)
cx_pem = models.ForeignKey(ProjectEngineeringManager, on_delete=models.DO_NOTHING,
verbose_name="CX PEM:", null=True,
blank=True
architect = models.ForeignKey(Architect, on_delete=models.DO_NOTHING,
verbose_name="CX Architect:", null=True, blank=True,
to_field="name"
Note that the "architect" field has "to_field" set None of the my other models needed this.
Hello ... i will check this code out..
never had to set to_field in my use cases... so there might be something .
@dgurovich Can you please also share with me a copy of your ReportView or ReportGenerator code ?
Hi! Back to this. Here is the view. I had used CSS to just blank it out, but now there's a use case for it.
class LBCbyPEMs(SlickReportView):
"""
Shows LBC Initiatives filtered by PEMs
"""
report_title = 'Epics Dashboard'
template_name = 'slick_reporting/lbc_report.html'
report_model = Initiative
date_field = 'req_date'
columns = ['lbc_unique_id', 'name', 'epic_status__status', 'c_x_pm', 'cx_pem__name',
'cx_tenet__acronym',
'epic_status__status', 'color_status__description']
def format_row(self, row_obj):
row_obj['lbc_unique_id'] = safe(
f"<a href=\"/lbc/{row_obj['lbc_unique_id']}/epic_main/\" style=\"color:#1295E5;\">"
f"{condition_id_digits(int(row_obj['lbc_unique_id']))}</a>")
row_obj['name'] = row_obj['name'][0:50]
return row_obj
Any progress on this? It's come up again and I'd like to not have to create a work-around.
Interestingly, the fields show up in the filter view, but the default values from the model are defaulted into the filters and not removeable! I had to remove the defaults in the model to make it work. When I set a filter though, just returns nothing.
Hello @dgurovich
I added a Foreign Key with a to_field set in my tests and all is passing and nothing seems off
I want to understand and be able to reproduce it...
You mean that the form select is not loading the expected values ?
Or it loads correct values but when you filter on ReportGeneratorView returns no data ?
The example above , the report view class does not have an 'architect' column or anything...
The filters show the default values in them with no way to remove. Also, the addition of any filter returns no items in the table.
Here is the model:
`class OKR(models.Model):
"""Business Object Mapping Objectives and Key Results to Epics"""
COMPLETE = 2
INCOMPLETE = 1
DEFAULT_VALUE_STREAM_CHOICE = "TBD"
KEY_RESULT_PROGRESS_CHOICES = (
(0, "0%"), (1, "1%"), (10, "10%"), (20, "20%"), (30, "30%"), (40, "40%"), (50, "50%"),
(60, "60%"), (70, "70%"), (80, "80%"), (90, "90%"), (100, "100%"),)
KEY_RESULT_STATUS_CHOICES = ((INCOMPLETE, "Incomplete"), (COMPLETE, "Achieved"),)
id = models.AutoField(primary_key=True)
key_result_progress = models.IntegerField(verbose_name="Key Result Progress:",
choices=KEY_RESULT_PROGRESS_CHOICES, default=0,
blank=False)
key_result_description = models.TextField(verbose_name="Key Result (description):", null=False,
blank=False)
value_stream = models.ForeignKey(
ValueStream, to_field="name", db_column="value_stream", verbose_name="Value Stream",
db_index=True, on_delete=models.DO_NOTHING, blank=False)
objective = models.ForeignKey(EpicObjective, to_field="name", db_column="objective",
verbose_name="Objective:", on_delete=models.DO_NOTHING,
blank=False)
lob = models.ForeignKey(LineOfBusiness, to_field="lob", db_column="lob",
verbose_name="Line of Business", db_index=True,
on_delete=models.DO_NOTHING, blank=False)
okr_progress = models.ForeignKey(OKRProgress, to_field="status", db_column="okr_progress",
verbose_name="OKR Progress",
on_delete=models.DO_NOTHING, blank=False)
product_area = models.ForeignKey(ProductArea, to_field="name", db_column="product_area",
verbose_name="Product Area", db_index=True,
on_delete=models.DO_NOTHING, blank=False)
key_result_status = models.IntegerField(verbose_name="Key Result Status:",
choices=KEY_RESULT_STATUS_CHOICES,
blank=False, default=INCOMPLETE)
start_date = models.DateField(verbose_name="Start Date: 📅", blank=True,
null=True,
help_text=globals.DATEPICKER)
due_date = models.DateField(verbose_name="Due Date: 📅", blank=True,
null=True,
help_text=globals.DATEPICKER)
start_value = models.IntegerField(verbose_name="Start Value", default=1,
validators=[MinValueValidator(1), MaxValueValidator(100)],
help_text=globals.ONE_ONE_HUNDRED_VALIDATOR_HELP)
current_value = models.IntegerField(verbose_name="Current Value", default=1,
validators=[MinValueValidator(1), MaxValueValidator(100)],
help_text=globals.ONE_ONE_HUNDRED_VALIDATOR_HELP)
target_value = models.IntegerField(verbose_name="Target Value", default=100,
validators=[MinValueValidator(1), MaxValueValidator(100)],
help_text=globals.ONE_ONE_HUNDRED_VALIDATOR_HELP)
class Meta:
"""
Metadata elements.
"""
managed = True
verbose_name = "OKR"
verbose_name_plural = "OKRs"
ordering = ['lob', 'value_stream']
def unassigned_okrs(self):
okrs = OKR.objects.filter(lob="Unassigned - TBD")
return okrs
def __str__(self):
"""
To String Function
:return: String Representation of model
"""
return f"LOB: {self.lob.lob} - Value Stream: {self.value_stream} - Impact: {self.product_area} - Product Lead: {self.lob.head_of_product}"
`
Hello !
Sorry but i have to ask you If you can send a print screen to share, that would be awesome if possible.
also the actual code of the problem Report View.
In the code, i can see i'm assuming '_id' field only in crosstab reports. is your report a crosstab one ?
Simple report. Vanilla
The first shot is no filters, The second one shows with a filter selected -- it should have returned quite a few.
interesting that the headers don't render html chars (fontawesome).
Letting you know that i got it on tests 💪 .. soon will be fixed i hope
The issue is fixed on the branch https://github.com/ra-systems/django-slick-reporting/tree/34_to_field_set_issues
Will be merging with the upcoming release
Hello ✋ Fixed and released, Thank you @dgurovich