Please consider this a book library website, I have three models here:
models.py
class books(models.Model):
book_name = models.CharField(verbose_name='book name',max_length=24)
book_desc = models.CharField(verbose_name='book description',max_length=240)
class classes(models.Model):
class_name = models.CharField(verbose_name='class name',max_length=24)
class map_book_class(models.Model):
book_id = models.ForeignKey(books, on_delete=models.CASCADE, verbose_name='book ID')
class_id = models.ForeignKey(classes, on_delete=models.CASCADE, verbose_name='class UID')
Here are some simple explains for the model:
John's Story(book name), Adventures(category).
My question is, after I successfully built up those three models, and I added following data by using the django admin page.
books
id book_name book_desc
1 John's Story a story for kids.
2 Best recipes good foods.
classes
id class_name
1 Adventures
2 Toolbooks
By using the third model, which should be able to map a book to a certain category together, but I realized that in the dropdown menu, it shows the id(primary key) instead of other information such as 'book_name' or others, like the screenshot below. screenshot
As you can see inside the screenshot, imagine that if I already have 100 different books and categorys in the database, I can not recognize them all just by their id, either for books and categorys. May I ask if there is any way to replace the default showing settings(by id[primary key]) to other fields in the dropdown menu, such as book_name or others? like this:screenshot 2
And I know that I can set unique ids, or use the book_name field as the primary key instead, since the default setting of this shows primary key field in the dropdown menu, but I want to make django automatic generate id for me for big amount of possible datas in the future use.
Thank you for answering! This is my first post on stackoverflow, hope I described my question clearly!
Several things you can use to improve it.
Book
instead of books
.Your models.py file could look like this:
class Book(models.Model):
name = models.CharField(verbose_name='book name',max_length=24)
desc = models.CharField(verbose_name='book description',max_length=240)
classes = models.ManyToManyField(
Class,
related_name='classes',
)
def __str__(self):
return self.name
class Meta:
ordering = ("name",)
class Class(models.Model):
name = models.CharField(verbose_name='class name',max_length=24)
def __str__(self):
return self.name
class Meta:
ordering = ("name",)
verbose_name = "Class"
verbose_name_plural = "Classes"