我有一个带有通用外键的简单模型:
class Generic(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
Run Code Online (Sandbox Code Playgroud)
我想过滤此表中具有非null content_object的所有条目,即过滤掉Generic其内容对象不再存在的所有实例:
Generic.objects.filter(~Q(content_object=None))
Run Code Online (Sandbox Code Playgroud)
这不起作用,给出例外:
django.core.exceptions.FieldError:字段'content_object'不生成自动反向关系,因此不能用于反向查询.如果是GenericForeignKey,请考虑添加GenericRelation.
添加GenericRelation到引用的内容类型模型没有区别.
如何实现这一点的任何帮助将不胜感激,非常感谢.
编辑:我意识到我可以级联删除,但在我的情况下这不是一个选项(我希望保留数据).
python django django-queryset django-generic-views django-contenttypes
Python(2.6)似乎无缘无故,任何人都可以看到这个代码有问题吗?
class DB ():
def doSomething (self, str):
print str
class A ():
__db = DB()
@staticmethod
def getDB ():
return A.__db
db = property(getDB)
A.db.doSomething("blah")
Run Code Online (Sandbox Code Playgroud)
失败,例外情况:
AttributeError:'property'对象没有属性'doSomething'
我的理解是,一个属性在访问时会自动运行它的getter,那么为什么它会抱怨一个属性对象,为什么它找不到我明显可用的方法呢?
我正在尝试创建一个简单的线程应用程序,其中我有一个执行一些长时间处理的方法和一个显示加载栏和取消按钮的小部件。
我的问题是,无论我如何实现线程,它实际上都不会线程化 - 一旦线程启动,UI 就会被锁定。我已经阅读了有关此问题的所有教程和帖子,现在我正在求助于社区尝试解决我的问题,因为我不知所措!
最初我尝试对 QThread 进行子类化,直到互联网说这是错误的。然后我尝试了 moveToThread 方法,但效果为零。
初始化代码:
loadingThreadObject = LoadThread(arg1)
loadingThread = PythonThread()
loadingThreadObject.moveToThread(loadingThread)
loadingThread.started.connect(loadingThreadObject.load)
loadingThread.start()
Run Code Online (Sandbox Code Playgroud)
PythonThread 类(显然 QThreads 在 pyQt 中存在 bug,除非你这样做,否则不会启动):
class PythonThread (QtCore.QThread):
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def start(self):
QtCore.QThread.start(self)
def run(self):
QtCore.QThread.run(self)
Run Code Online (Sandbox Code Playgroud)
加载线程类:
class LoadThread (QtCore.QObject):
results = QtCore.Signal(tuple)
def __init__ (self, arg):
# Init QObject
super(QtCore.QObject, self).__init__()
# Store the argument
self.arg = arg
def load (self):
#
# Some heavy lifting is done
#
loaded = True
errors = …Run Code Online (Sandbox Code Playgroud) 在通过相关字段进行过滤时,如何从自定义管理器查询集应用注释和过滤器?这里有一些代码来证明我的意思.
经理和模特
from django.db.models import Value, BooleanField
class OtherModelManager(Manager):
def get_queryset(self):
return super(OtherModelManager, self).get_queryset().annotate(
some_flag=Value(True, output_field=BooleanField())
).filter(
disabled=False
)
class MyModel(Model):
other_model = ForeignKey(OtherModel)
class OtherModel(Model):
disabled = BooleanField()
objects = OtherModelManager()
Run Code Online (Sandbox Code Playgroud)
尝试使用管理器过滤相关字段
# This should only give me MyModel objects with related
# OtherModel objects that have the some_flag annotation
# set to True and disabled=False
my_model = MyModel.objects.filter(some_flag=True)
Run Code Online (Sandbox Code Playgroud)
如果您尝试上面的代码,您将收到以下错误:
TypeError: Related Field got invalid lookup: some_flag
为了进一步澄清,基本上相同的问题被报告为一个错误,没有回应如何实际实现这个:https://code.djangoproject.com/ticket/26393.
我知道这可以通过直接在MyModel过滤器中直接使用管理器中的过滤器和注释来实现,但重点是保持此DRY并确保在访问此模型的任何地方重复此行为(除非明确指示不要) …
有没有办法在python中使用变量字符串访问变量?例如,我想要比eval以下方式更简洁:
def toggleListButtons (self):
buttons = ["flip", "remove", "removeAll", "delete", "deleteAll", "loadDirectory"]
for button in buttons:
eval("self." + button + "Button.setEnabled(!self." + button + "Button.isEnabled())")
Run Code Online (Sandbox Code Playgroud) python ×5
django ×2
class ×1
django-orm ×1
eval ×1
exception ×1
object ×1
properties ×1
pyqt ×1
variables ×1