简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM