I am using django v1.10.2
I am trying to create dynamic reports whereby I store fields and conditions and the main ORM model information into database.
My code for the generation of the dynamic report is
class_object = class_for_name("app.models", main_model_name)
results = (class_object.objects.filter(**conditions_dict)
.values(*display_columns)
.order_by(*sort_columns)
[:50])
So main_model_name
can be anything.
This works great except that sometimes associated models of the main_model
have choicefield
.
So for one of the reports main_model
is Pallet
.
Pallet
has many PalletMovement
.
My display columns are :serial_number
, created_at
, pallet_movement__location
The first two columns are fields that belong to Pallet
model.
The last one is from PalletMovement
What happens is that PalletMovement
model looks like this:
class PalletMovement(models.Model):
pallet = models.ForeignKey(Pallet, related_name='pallet_movements',
verbose_name=_('Pallet'))
WAREHOUSE_CHOICES = (
('AB', 'AB-Delaware'),
('CD', 'CD-Delaware'),
)
location = models.CharField(choices=WAREHOUSE_CHOICES,
max_length=2,
default='AB',
verbose_name=_('Warehouse Location'))
Since the queryset will return me the raw values, how can I make use of the choicefield
in PalletMovement
model to ensure that the pallet_movement__location
gives me the display of AB-Delaware
or CD-Delaware
?
Bear in mind that the main_model
can be anything depending on what I store in the database.
Presumably, I can store more information in the database to help me do the filtering and presentation of data even better.