Creating a new table with request data

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

class UserAPIview(APIView):
    def get(self, request):
        musers = Users.objects.all()
        serializer = UserGetSerializer(musers, many=True)
        return Response(, status=status.HTTP_200_OK)

    def post(self, request):
        serializer = UserPostSerializer(
        if serializer.is_valid():
            return Response(, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Hi, welcome to Django and the forum!

Typically you wouldn’t want to create new tables. Instead, you’d use a different table design.

For example, you could have these models:

class Game(models.Model):

class GameWord(models.Model):
    game = models.ForeignKey(Game)
    word = models.CharField(max_length=120)
    class Meta:
       constraints = [models.UniqueConstraint(fields=['game', 'word'])]

Then you can get all the GameWords for a particular game by querying like so:

for gw in GameWord.objects.filter(game=some_game):

If you aren’t controlling your schema with Django migrations yet, you should consider doing so - it will save you some work translating between SQL and your model code.


Yes that’s way more efficient… don’t know why that didn’t occur to me :smile:
Thank you!

As for the migrations, I’m assuming you mean the makemigrations and migrate commands? I’ve been using them to import the changes to the database but my understanding is still too vague to make any sense of what else it means :stuck_out_tongue:

Okay I misunderstood - I thought you were manually managing your tables with SQL.

1 Like