0

I have 2 tables and wanted to combine 1st and 2nd table and show it in 3rd table

   class Date(models.Model):
        name = models.CharField(max_length=50)

        def __unicode__(self):
            return self.name

   class Month(models.Model):
        name = models.CharField(max_length=50)

        def __unicode__(self):
            return self.name

I have my 3rd table which is combination of 1st and 2nd table

I tried to use models.ForeignKey(Date,Month) in my 3rd table but only Month table data was shown in admin panel Date table data was not populated

My 3rd table looks like this

class Group(models.Model):
    name = models.ForeignKey(Month,Date)

    def __unicode__(self):
        return self.name

I there any alternate way of populating data from different tables?

Any help is populating data of different tables in one table is very helpfull

Thanks in advance

Coeus
  • 2,385
  • 3
  • 17
  • 27
  • Possible duplicate of [Display objects from different models at the same page according to their published date](http://stackoverflow.com/questions/37747427/display-objects-from-different-models-at-the-same-page-according-to-their-publis) – e4c5 Sep 15 '16 at 12:31

1 Answers1

2

To get 1st and 2nd table content into 3rd table you need to write separate foreign keys for each model. We should not assign a single foreign key for both models.

class Group(models.Model):
    date = models.ForeignKey(Date)
    month = models.ForeignKey(Month)

    def __unicode__(self):
        return str(self.date.name) + str(seld.month.name)

The above code will return both Date and Month names.

MicroPyramid
  • 1,570
  • 19
  • 22
  • Thanks for the reply. But this only gives value while querying in views. I wanted the data to be present in model (my 3rd table will have combination of PK of 2 tables as a drop down in admin panal) – Coeus Sep 15 '16 at 13:13