user7210105
user7210105

Reputation:

Django Admin hides names and shows (table_name object)

I am fairly new to django admin. I am trying to manage my table contents from the admin section for table created with the following models:

class subject(models.Model):
   subject_id = models.CharField(max_length=12, unique=True)
   name =  models.CharField(max_length=25)

class subject_date(models.Model):
   sub_id = models.ForeignKey(subject)
   date =  models.CharField(max_length=25)

I have added my subjects to the subject table and now I want to add subject dates to the subject_date table from the admin section. Because of the one to many relationship I get a drop-down list under sub_id of all the subjects I added in the subject table. the problem is that the drop-down list has all the contents written as follows:

subject object

This makes it impossible for me to see which subject I am dealing with. Can anyone please help me with this if its an issue that can be fixed.

Upvotes: 0

Views: 727

Answers (1)

MicroPyramid
MicroPyramid

Reputation: 1630

In your models.py define __str__ and return your name.

class subject(models.Model):
   subject_id = models.CharField(max_length=12, unique=True)
   name =  models.CharField(max_length=25)

   def __str__(self):
        return self.name

Upvotes: 2

Related Questions