views:

141

answers:

0

The behavior was unrelated to the problem as presented immediately below. See the bottom of the post for an explanation. thanks.


Hello,

I am currently experiencing the behavior that the default Manager for a particular Model returns the objects for this Model only once per request or per shell session. Below is a PDB transcript from stopping in a view (but the behavior occurs without PDB, too):

#Nothing up my sleeves (using the default Manager):
(Pdb) p Entry.objects
<django.db.models.manager.Manager object at 0x18523b0>

#Now you see them...
(Pdb) Entry.objects.all()
[<Entry: Entry 1>, <Entry: Entry 2>, <Entry: Entry 3>, <Entry: Entry 4]

#Now you don't!
(Pdb) Entry.objects.all()
[]

Anytime I retrieve an object, that object no longer appears in later QuerySets.

#(New Request from above)

#If I only request one object then it is the one that disappears
(Pdb) Entry.objects.all()[0]
[<Entry: Entry 1>]

#Here Entry 1 is missing
(Pdb) Entry.objects.all()
[<Entry: Entry 2>, <Entry: Entry 3>, <Entry: Entry 4]

#And now they're all gone.
(Pdb) Entry.objects.all()
[]

This behavior is only for one of my models; the other models seems to query correctly. I don't think that it has anything to do with my Model's definition, which is basicaly:

class Entry(models.Model):
    user = models.ForeignKey(User, related_name='entries')
    blog = models.ForeignKey(Blog, related_name='entries')
    positive = models.BooleanField()

I apologize that my description is a little vague; I'm confused about how this behavior could arise and not sure where to poke around next.

The SQL generated by the Manager for the QuerySet is the same (and apparently correct) each time:

(Pdb) p Entry.objects.all().query.as_sql()
('SELECT "myapp_entry"."id", "myapp_entry"."user_id", "myapp_entry"."blog_id", "myapp_entry"."positive" FROM "myapp_entry"', ())
(Pdb) p Entry.objects.all()
[<Entry: Entry 1>, <Entry: Entry 2>, <Entry: Entry 3>, <Entry: Entry 4]

(Pdb) p Entry.objects.all().query.as_sql()
('SELECT "myapp_entry"."id", "myapp_entry"."user_id", "myapp_entry"."blog_id", "myapp_entry"."positive" FROM "myapp_entry"', ())
(Pdb) Entry.objects.all()
[]

I'm using Django 1.0.2, Python 2.6.1, and the SQLite that came packaged with Python 2.6.1 on a Mac OS 10.5 machine.

In response to one of the comments I tried renaming the related_name parameters to entries1 and entries2 to avoid a possible conflict but this did not change the behavior.


SOLVED (I think)

Sorry all, the problem was actually unrelated to the problem as I presented it. I had a careless bug in one of my signals on Entry:

In myapp.__init__:

post_init.connect(entry_initialized, sender=Entry)

In myapp.signals:

def entry_initialized(sender, instance, *args, **kwargs):
    try:
        #Disconnect signal to avoid infinite recursion
        post_init.disconnect(entry_initialized, sender=Entry)

        #Intializing a new Entry here would cause the recursion
        #Check to see if there is a previous entry by this user in this blog
        #In this app entries are unique by (User, Blog)
        #And what we want to do is remove the old Entry if the positive fields don't match
        prev_instance = Entry.objects.get(user=instance.user, blog=instance.blog)

        #This is an error: it is not appropriate to delete without some checking
        prev_instance.delete()

        post_init.connect(verification_initialized, sender=Verification)
    except:
        post_init.connect(verification_initialized, sender=Verification)

The correct version of my code would be:

        #Only delete if this is a different Entry with same user/blog and the positive field is different.
        if not instance.id == prev_instance and not instance.positive == prev_instance.positive:
            prev_instance.delete()