DEV Community

Purnima Chowrasia
Purnima Chowrasia

Posted on

#2 Django Journey: Learn DRF by building an e-commerce APIs

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:

  1. Create a Category model.
  2. Modified the existing Product model to add category as Foreign key field.
  3. Applied Database migration.
  4. Added Category serializer, with a addon serializer method inside the respective serializer class to get product count for a particular category.
  5. Modified Product serializer to show category info as nesting category serializer.
  6. Created APIs to handle CRUD operation on Category.
  7. Registered both Product and Category models on Django admin interface for easy data management.
  8. 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):

  1. Deleting migration file which got created when executing makemigrations command.
  2. Commented out category field(Foreign key) in Product model.
  3. Applied migration only for creating Category model in Database.
  4. Then added data in Category model using shell command.
  5. Uncommented, category field(Foreign key) in Product model.
  6. Applied migrations again, it asked for some default value to be added in category field in existing product data. Chose the option 1.
  7. 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.
Django Admin panel

Complete code available here.

Next, I will be working on User Authentication. See you next time..bye👋

Top comments (0)