관리자는 요청 당 모델 객체를 반환합니다 (관리자는 객체를 한 번 반환 한 후 객체를 떨어 뜨립니다)
-
03-07-2019 - |
문제
행동은 바로 아래에 제시된대로 문제와 관련이 없었다. 설명은 게시물의 하단을 참조하십시오. 감사해요.
안녕하십니까,
현재 특정 모델의 기본 관리자가 요청 당 또는 쉘 세션 당 한 번만이 모델의 객체를 반환하는 동작을 경험하고 있습니다. 아래는 PDB 성적표가 뷰에서 멈추지 않도록하는 것입니다 (그러나 동작은 PDB 없이도 발생합니다).
#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()
[]
객체를 검색 할 때마다 해당 객체가 더 이상 쿼리 세트에 나타나지 않습니다.
#(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()
[]
이 동작은 내 모델 중 하나에만 해당됩니다. 다른 모델은 올바르게 쿼리하는 것 같습니다. 나는 그것이 내 모델의 정의와 관련이 있다고 생각하지 않습니다. 이것은 기본적입니다.
class Entry(models.Model):
user = models.ForeignKey(User, related_name='entries')
blog = models.ForeignKey(Blog, related_name='entries')
positive = models.BooleanField()
내 설명이 조금 모호하다는 사실에 사과드립니다. 나는이 행동이 어떻게 일어날 수 있는지 혼란스럽고 다음 주위에 어디에서 찌를 곳을 확신하지 못합니다.
QuerySet의 관리자가 생성 한 SQL은 매번 동일하며 명백하게 정확합니다.
(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()
[]
Django 1.0.2, Python 2.6.1 및 Mac OS 10.5 기계에서 Python 2.6.1로 포장 된 SQLITE를 사용하고 있습니다.
내가 바꾸려고 시도한 의견 중 하나에 대한 응답으로 related_name
매개 변수 entries1
그리고 entries2
가능한 갈등을 피하기 위해서는 행동이 바뀌지 않았습니다.
해결되었습니다 (생각합니다)
모두 죄송합니다. 문제는 제가 제시 한 것처럼 실제로 문제와 관련이 없었습니다. 입력시 신호 중 하나에 부주의 한 버그가있었습니다.
~ 안에 myapp.__init__
:
post_init.connect(entry_initialized, sender=Entry)
~ 안에 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)
내 코드의 올바른 버전은 다음과 같습니다.
#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()
해결책
모두 죄송합니다. 문제는 제가 제시 한 것처럼 실제로 문제와 관련이 없었습니다. 입력시 신호 중 하나에 부주의 한 버그가있었습니다.
~ 안에 myapp.__init__
:
post_init.connect(entry_initialized, sender=Entry)
~ 안에 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)
내 코드의 올바른 버전은 다음과 같습니다.
#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()