Referencing an object of a model with all foreign keys in Django admin UI
<p>I am developing my Djnago app for processing data into DB from admin frontend. 
I have a table with both foreign keys in it.</p>

<p><code>class exam_questions(models.Model):
    exam_id=models.ForeignKey(exams, on_delete=models.CASCADE)
    question_id=models.ForeignKey(questions, on_delete=models.CASCADE)
    def __int__(self):
        return self.exam_id</code></p>

<p>When I am trying to return the above field it is shown in admin UI as exam_questions object for all values added into table. 
Is there a way to get the actual value of the column displayed in admin UI.</p>

<p>I have other tables which have different definitions and I am able to display the required field from that. The issue is observed only when all keys in model are foreign keys</p>

<p>Any help is appreciated.</p>

<p>You need to provide the <code>__str__</code> or <code>__unicode__</code> functions, with regard to your python version, in a model for Django admin to be able to list items with their proper intended information.</p>

<pre><code>@python_2_unicode_compatible
class exam_questions(models.Model):
    exam_id=models.ForeignKey(exams, on_delete=models.CASCADE)
    question_id=models.ForeignKey(questions, on_delete=models.CASCADE)

    def __int__(self):
        return self.exam_id

    def __str__(self):
        return '%s - %s' % (self.exam_id.name, self.question_id.name)
</code></pre>

<p>If you're coding with cross version compatibility, you need to decorate your model with <code>@python_2_unicode_compatible</code>, and only override the <code>__str__</code> method.</p>

<p>Anyways but, why your <code>ForeignKeys</code> have <em>_id</em> in them ? Django automatically adds the <em>_id</em> to FKs that points to the id of the related instance.</p>

python mysql django