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:
- The first model is for adding new books, including it's name and book description, the model id will be generated automaticly by django.
- The second model is for adding new categories for the book, for example: recipes, toolbooks... etc. The model id will be generated automaticly by django too.
- The third model is for a mapping table, which connects the book data added by the first model, and the category data added by the second model together by using ids from above two models as foreign keys. After this, the data should be mapped together like this:
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!
CodePudding user response:
Several things you can use to improve it.
- Try using CapWords for Django model names (https://peps.python.org/pep-0008/#class-names). Use
Book
instead ofbooks
. - Use many to many relationship for the book classes instead of creating the relations on your own
- Use str() method to get human readable object names for your model objects
- Read Model Meta options for further model improvements
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"