user3033194
user3033194

Reputation: 1831

objects.all() not returning any objects in Django

I have the following models in models.py:

from django.contrib.auth.models import User
from django.db import models

class Usertypes(models.Model):
    user = models.OneToOneField(User)
    usertype = models.TextField()                   

    def __unicode__(self):
        return self.user_name

class Games(models.Model):
    name = models.CharField(max_length=100,unique=True)
    category = models.CharField(max_length=100)
    url = models.URLField()
    developer = models.ForeignKey(User)
    price = models.FloatField()

    def __unicode__(self):
        return self.name

class Scores(models.Model):
    game = models.ForeignKey(Games)
    player = models.ForeignKey(User)
    registration_date = models.DateField(auto_now=False, auto_now_add=False)
    highest_score = models.PositiveIntegerField(null=True,blank=True)
    most_recent_score = models.PositiveIntegerField(null=True,blank=True)

    def __unicode__(self):
        return self.most_recent_score

I am now creating objects of all 3 types. I have created some User and Games objects earlier, so when I run the following commands, the outputs as given below are obtained:

>>> u = User.objects.all()
>>> u.count()
5
>>> g = Games.objects.all()
>>> g.count()
9

Now, I am trying to create some Scores objects using the following commands. The outputs are given below:

>>> fifa = Games.objects.get(pk=18)
>>> user1 = User.objects.get(id=2)
>>> user2 = User.objects.get(id=7)
>>> user3 = User.objects.get(id=9)
>>> p1 = Scores(game=fifa,player=user1,registration_date='2015-01-29')
>>> p2 = Scores(game=fifa,player=user2,registration_date='2014-12-21')
>>> p3 = Scores(game=fifa,player=user3,registration_date='2015-01-29')
>>> user1
<User: admin>
>>> fifa
<Games: Games object>
>>> p1
<Scores: Scores object>
>>> p2
<Scores: Scores object>
>>> p3
<Scores: Scores object>

The problem is:

>>> s = Scores.objects.all()
>>> s.count()
0

I don't understand why, despite having created 3 Scores objects, Scores.objects.all() returns nothing. Can someone help? Thanks in advance!!

Upvotes: 2

Views: 2657

Answers (2)

Aadesh
Aadesh

Reputation: 162

Make sure you are running .save(), which actually saves the item in the database. Without it, you aren't saving anything in the database therefore you get nothing when you retrieve objects from the database

Upvotes: 1

Thomas Orozco
Thomas Orozco

Reputation: 55303

You never inserted the scores to the database (so of course the database can't give them back to you):

p1.save()  # This will save the object to the database

Note that you could also use Scores.objects.create(...) (instead of Score()). This will initialize an object and immediately save it to the database:

p1 = Scores.objects.create(game=fifa,player=user1,registration_date='2015-01-29')

Now these methods would result in three queries being made to the database, which isn't ideal (you hit the roundtrip latency 3 times).

Fortunately, you can easily optimize and make a single query using bulk_create:

Scores.objects.bulk_create([
    Scores(game=fifa,player=user1,registration_date='2015-01-29')
    Scores(game=fifa,player=user2,registration_date='2014-12-21')
    Scores(game=fifa,player=user3,registration_date='2015-01-29')
])

Upvotes: 2

Related Questions