[Django]-How to use a python class with data objects in mysql

4👍

You are correct that you call the save() method to save models to your db, But you don’t have to define the save method within your model classes if you don’t want to. It would be extremely helpful to go through the django tutorial which explains all.

https://docs.djangoproject.com/en/dev/intro/tutorial01/

https://docs.djangoproject.com/en/dev/topics/db/models/
Explains django models

django uses its own ORM (object-relational mapping)

This does exacxtly what it sounds like maps your django/python objects (models) to your backend.

It provides a sleek, intuitive, pythonic, very easy to use interface for creating models (tables in your rdbms) adding data and retrieving data.

First you would define your model

class Player(models.Model):
  points = models.IntegerField()
  name = models.CharField(max_length=255)

django provides commands for chanign this python object into a table.

python manage.py syncdb

you could also use python manage.py sql <appname> to show the actual sql that django is generating to turn this object into a table.

Once you have a storage for this object you can create new ones in the same manner you would create python objects

new_player = Player(points=100, name='me')
new_player.save()

Calling save() actually writes the object to your backend.

1👍

You’re spot on…

Start at https://docs.djangoproject.com/en/dev/intro/tutorial01/

Make sure you have the python bindings for MySQL and work your way through it… Then if you have specific problems, ask again…

Leave a comment