relationship(foreign key or many to many) betweeen the creating from the frontend to the backend

59 views
Skip to first unread message

David James Nwoyie

unread,
Oct 27, 2024, 1:49:19 AM10/27/24
to Django users
good morning,  everyone  i have this project that I'm working on ,the project is foodapp where food vendor can register their eatery and customers will register and then  place their order  I'm using django,drf for backend and nextjs for frontend but the problem I'm having is that,I can't create something with that has relation shipwith other tables in the django model for example the restaurant and menu item has relation which is foreignkey  but when i create the  menu item from the frontend it will return 500 or 401 but when I create with drf it works, how can i connect my backend  with tables that share realtionship(foreignkey) to the frontend so that i can post(create) from the frontend to the backend

Rishabh Shukla

unread,
Oct 27, 2024, 10:06:00 AM10/27/24
to django...@googlegroups.com
Try using nested serializer or switch to graphql api for this specific task.

On Sun, Oct 27, 2024, 12:50 AM David James Nwoyie <jamesnwo...@gmail.com> wrote:
good morning,  everyone  i have this project that I'm working on ,the project is foodapp where food vendor can register their eatery and customers will register and then  place their order  I'm using django,drf for backend and nextjs for frontend but the problem I'm having is that,I can't create something with that has relation shipwith other tables in the django model for example the restaurant and menu item has relation which is foreignkey  but when i create the  menu item from the frontend it will return 500 or 401 but when I create with drf it works, how can i connect my backend  with tables that share realtionship(foreignkey) to the frontend so that i can post(create) from the frontend to the backend

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/ef66e6ed-afba-4ab8-b5f7-3511e548df3an%40googlegroups.com.

Ryan Nowakowski

unread,
Oct 27, 2024, 11:41:02 AM10/27/24
to django...@googlegroups.com
On Sat, Oct 26, 2024 at 10:49:18PM -0700, David James Nwoyie wrote:
> but when i create the menu item from the frontend it will
> return 500 or 401 but when I create with drf it works

When you say "with drf" do you mean using the API via command line(ex:
curl) or using the Django Admin?

maina patrick

unread,
Oct 27, 2024, 2:39:38 PM10/27/24
to django...@googlegroups.com

Good morning! It sounds like you’re making solid progress on your FoodApp project. The issue you’re facing with creating menu items that have foreign key relationships can often happen due to authentication or serialization issues in Django and DRF. Here’s a breakdown to help resolve it:

  1. Verify Authorization (401 Error): Ensure your frontend is sending the necessary authorization headers (like JWT token or session cookies) required to access your DRF views. For example, use axios or fetch to attach these headers properly.

  2. Check Data Format (500 Error): Confirm that the payload sent from the frontend matches the expected structure in your DRF serializer. With foreign keys, make sure to send either:

    • The primary key of the related object (e.g., restaurant_id), or
    • Use DRF’s nested serializers if you’re submitting a complete related object.
  3. Adjust Serializers: In DRF, specify how you handle related objects in your serializers:

    python
    class MenuItemSerializer(serializers.ModelSerializer): restaurant_id = serializers.PrimaryKeyRelatedField(queryset=Restaurant.objects.all()) class Meta: model = MenuItem fields = ['name', 'price', 'restaurant_id']
  4. Debugging: Set up error logging for detailed error messages, especially if Debug is False. In Django, custom error messages can help identify why the 500 error occurs.

  5. Testing with DRF: Since it works in DRF but not from the frontend, inspect the network request in your browser's DevTools to ensure the frontend request matches the successful DRF request format.

Let me know if you need further help with any specific part!


On Sun, Oct 27, 2024 at 8:49 AM David James Nwoyie <jamesnwo...@gmail.com> wrote:
good morning,  everyone  i have this project that I'm working on ,the project is foodapp where food vendor can register their eatery and customers will register and then  place their order  I'm using django,drf for backend and nextjs for frontend but the problem I'm having is that,I can't create something with that has relation shipwith other tables in the django model for example the restaurant and menu item has relation which is foreignkey  but when i create the  menu item from the frontend it will return 500 or 401 but when I create with drf it works, how can i connect my backend  with tables that share realtionship(foreignkey) to the frontend so that i can post(create) from the frontend to the backend

--
Reply all
Reply to author
Forward
0 new messages