2
models.py

class DailyRecordManager(models.Manager):
def get_query_set(self):
    qs = super(DailyRecordManager, self).get_query_set().order_by('date_organised')
    return qs

class DailyRecord(models.Model):
    date_organised = models.DateField('Ogransied Date', help_text=('Enter Date when the program is organised: CCYY-MM-DD'))
    program_name = models.TextField('program name',)
    venue = models.CharField('venue', max_length = 255, blank=True)
    organiser = models.ForeignKey(Organiser, verbose_name = 'Organiser', related_name = 'organisers')

    objects = models.Manager()
    public = DailyRecordManager()


    class Meta:
        verbose_name = 'dailyrecord'
        verbose_name_plural = 'dailyrecords'
        ordering = ['-date_organised']

    def __str__(self):
         return self.program_name

class Participant(models.Model):
    participant = models.CharField(max_length= 50, unique = True)
    daily_record = models.ForeignKey(DailyRecord, verbose_name = 'program_name')

    class Meta:
        verbose_name = 'participant'
        verbose_name_plural = 'participants'

    def __str__(self):
        return self.participant

views.py

class DailyActivityPageView(SingleTableView):
    table_class = DailyRecordTable
    queryset = DailyRecord.public.all() 
  # queryset = Participant(DailyRecord.public.all()) is not working
    template_name = 'dailyrecord/daily-activity-record.html'

tables.py

class DailyRecordTable(tables.Table):
    date_organised = tables.Column('Date')
    program_name = tables.Column( 'Program Name')
    venue = tables.Column( 'Venue')
    organiser = tables.Column( 'Organiser')
    participant = tables.Column( 'dailyrecord.participant')

# daily = tables.RelatedLinkColumn()

    class Meta:
        model = DailyRecord

Now what I need is to display the data from participant table too, corresponding to the daily_record foreign key.

Click this link to view the snapshot. see the last column of the table. I need the data of Participant.partcipant column here

Sorry for poor English.

Thank You

Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48
Anuj Subedi
  • 42
  • 1
  • 8

1 Answers1

0

There are two problems here.

First is, that a daily record can have multiple participants. Thus, in order to fill last column you have to aggregate all possible participants into that column (for example as list).

Second is, that you should make Participant backwards related to DailyRecord by adding attribute "related_name" to daily_record in your Participant model, like this:

daily_record = models.ForeignKey(DailyRecord, verbose_name = 'program_name', related_name="participants")

Now, you should simply get participants from daily_record like this:

participants = DailyRecord.public.first().participants.all()

If you had OneToOneField instead of ForeignKey you could add (single) participant to table like this:

participant = tables.Column( "Participant", accessor='participant')
Henhuy
  • 1,034
  • 1
  • 13
  • 23