Hey dev.to community!
In continuation to my previous post, where I mentioned about working on building Products app, CRUD operation related to Products. Now, here is the current progress that I wanted share with you all:
- Create a Category model.
- Modified the existing Product model to add category as Foreign key field.
- Applied Database migration.
- Added Category serializer, with a addon serializer method inside the respective serializer class to get product count for a particular category.
- Modified Product serializer to show category info as nesting category serializer.
- Created APIs to handle CRUD operation on Category.
- Registered both Product and Category models on Django admin interface for easy data management.
- Created superuser and interacted with Django Admin interface.
While applying database migration, I encountered an issue as I have some data already added as Products. And no data under Category were available. Here is how I solved this(definitely with the help of prompting LLM):
- Deleting migration file which got created when executing
makemigrations
command. - Commented out category field(Foreign key) in Product model.
- Applied migration only for creating Category model in Database.
- Then added data in Category model using shell command.
- Uncommented, category field(Foreign key) in Product model.
- Applied migrations again, it asked for some default value to be added in category field in existing product data. Chose the option 1.
- And issue sorted. I believe, there can be other ways to sort this issue.
Overall, it was a great experience to till now and hoping to keep going like this. Attaching ss of Django admin panel.
Complete code available here.
Next, I will be working on User Authentication. See you next time..bye👋
Top comments (0)