Jay P.
Jay P.

Reputation: 2590

How can I update data in Django ORM or Django shell

I'm very new to Django, so I'm trying to figure out some useful ways to handle data in my website automatically.

What I wanna do is the following:

  1. Get existing data from the database.
  2. Based on the data, I do some calculations on the given data to make new data.
  3. After getting the new data, I put them in the sqlite database.

Let's say I want to modify all my users's score points to be zero every month. In this case, it's gonna be automatic.

Also, let's say I want to ban users with their scores being zero at a specific day every month. This case will be done manually.

I don't know if those better need to be done Django ORM or Django shell. Can anyone help me figure out a useful way for this?

Upvotes: 0

Views: 4375

Answers (1)

Ramtin
Ramtin

Reputation: 3205

Your answer is way too general and you better be watching some tutorials. However, to answer your question:
1. after creating an app, there will be a file in each app called models.py. This file is intended to create a table inside your database, SQLite or otherwise. Take this as an example:

class Profile (models.Model):
    mobile = models.IntegerField(default=0, blank=True)
    country = models.CharField(max_length=100, blank=True)

You can find the documentation here
2. To access the data from the database's table, in your views.py, you have to import the table from .models import Profile
3. to get the data, you have many ways. This is one of them:

profile = Profile.objects.get(country='Germany')
print(profile.mobile)

You can find the documentation here
4. after getting the data, you can change it however you want. But, the data is not going to get updated automatically in the database. For changing (or creating new) rows in the database, you have to call save() function.

profile_2 = Profile(mobile=123, country="England")
profile.save()

or

profile = Profile.objects.get(pk=12)
profile.country = "France"
profile.save()

You can find a better explanation of the database API in this video

Edit

After your comment: what you are asking is a bit complicated, not because of the calculation but because of the design. This is not the practical design but the simple one.
models.py:

class Student(models.Model):
    average = models.IntegerField(default=0, null=True)

class Course(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    point = models.IntegerField(default=0)

views.py:

from .models import Student, Course

student1 = Student()
student1.save()
course_a = Course(student=student1, point=10)
course_b = Course(student=student1, point=20)
course_c = Course(student=student1, point=20)
course_a.save()
course_b.save()
course_c.save()

a_point = course_a.point
b_point = course_b.point
c_point = course_c.point
count = Course.objects.filter(student=student1).count()
student1.average = (a_point + b_point + c_point) / count
student1.save()

Upvotes: 2

Related Questions