Django combine different tables in separate table
<p>I have 2 tables and wanted to <code>combine</code> 1st and 2nd table and show it in 3rd table</p>

<pre><code>   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
</code></pre>

<p>I have my 3rd table which is combination of 1st and 2nd table</p>

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

<p>My 3rd table looks like this</p>

<pre><code>class Group(models.Model):
    name = models.ForeignKey(Month,Date)

    def __unicode__(self):
        return self.name
</code></pre>

<p>I there any alternate way of populating data from different tables?</p>

<p>Any help is populating data of different tables in one table is very helpfull</p>

<p>Thanks in advance</p>

<p>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.</p>

<pre><code>class Group(models.Model):
    date = models.ForeignKey(Date)
    month = models.ForeignKey(Month)

    def __unicode__(self):
        return str(self.date.name) + str(seld.month.name)
</code></pre>

<p>The above code will return both Date and Month names.</p>

python django django-models