0

I am desperately waiting for someone attention to get my question answered.... please help.. ModelAdmin model has to export to Excel action method.

I need to access related model fields in action method. That means I can not pass any arguments therefore I tried relatedmodel_set but ModelAdmin action method shows memory location and fails when I try to access values through attributes: <django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager..RelatedManager object at 0x7f8eea904ac0>

model.py

class EnrolStudent(models.Model):

  def get_trn_activity(self):
      return self.studenttraininactivities_set


class StudentTraininActivities(models.Model):

    trainin_activities = models.ForeignKey(EnrolStudent, 
                              on_delete=CASCADE, null=True )
    <other fields...>

admin.py

@admin.register(EnrolStudent)
class EnrolAdmin(admin.ModelAdmin):
    form = CityInlineForm
    inlines = [CohortTraininActivitiesInline]

    ...
    actions = [export_as_txt_action_0120("File NAT00120 data Export"
            , fields=['information_no', 'get_trn_activity',
            'student_enrol__student_code'])]

I need to access related model fields to export to excel. I can not pass parameter to get_trn_activity as you have noticed. Therefore selected rows only data from Django admin change_list page will only need bit of work using its queryset in actions method used in separate actions.py file that I can do!

Please help me with this issue. I am new to Python / Django. I also tried property decorator in related model then access in a method in main model then call it inside action but same problem with memory address not the direct value and then how to get data of memory location here .... I don't know.

If I can access the related fields then I can do it no issue.

Another question: I had same situation with model/related model before, but they were connected through OneToOneField relationship and I was able to use dundor to access related model fields but in this case of ForiegnKey relationship I can not see related model in queryset.

In other case this is what I can do easily; here cohortdetails is related model and when I debug I saw it was listed in queryset that was great.

actions = [export_as_txt_action_0080("File NAT00080 txt Export", fields=['rto_student_code', 'first_name', 'family_name' ,'cohortdetails__highest_school__highestschool_levelcode', 'cohortdetails__cohort_gender' , 'cohortdetails__student_dob' ])]

G-2
  • 11
  • 1
  • 5

0 Answers0