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 Copy code 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 < jamesnwoyieda...@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+unsubscr...@googlegroups.com. > To view this discussion visit > https://groups.google.com/d/msgid/django-users/ef66e6ed-afba-4ab8-b5f7-3511e548df3an%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/ef66e6ed-afba-4ab8-b5f7-3511e548df3an%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/django-users/CA%2B7S-Xv-41hyc7Hp7f6wosx9pjSCEbe9Y75FyCJ68kkE6sbnGQ%40mail.gmail.com.