So I’m trying to learn the basics of django. I am now creating a server side application that returns json responses via http requests.
I am making a Hangman game backend with pgsql. The backend will be used with a python logic layer and forntend text interface that I have already created as part of a previous project that I am now expanding on. I will not be using any HTML for now. So purely backend.
What I’ve done so far:
- Set up the pgsql host through elephantsql and connected to it via pgadmin
- Created basic models for tables that are already defined
- Created Model serializers GET/POST/DELETE for those models
- Created some basic APIviews with the REST framework to test the functionality
- It works
What I need to figure out:
In the game, it should be possible to create a new word list to play (a table containing words) via a request by sending a Tablename along with a username to the backend in json format. The backend should add the Tablename and Username to a table that holds all word lists along with usernames. Furthermore the backend should create a new table with the requested tablename. The user can then add new words to the new table, remove words from the table and delete the table completely (if he is authorized to do so)
I am not sure how to accomplish this since Django relies on the Model classes. Will I have to implement all queries for modifying the new table in raw sql in the views? How would you approach this?
Here is an example of how I am handling the requests in views.py:
class UserAPIview(APIView):
def get(self, request):
musers = Users.objects.all()
serializer = UserGetSerializer(musers, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request):
serializer = UserPostSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)