[英]Django select_related returning empty query set if the PK deleted from other table
Consider I am having a table that having a foreignkey relation with the user model.假设我有一个与用户 model 具有外键关系的表。
class MyModel(models.Model):
created_user = models.ForeignKey(settings.AUTH_USER_MODEL)
I am taking queryset like this我正在接受这样的查询集
qs = MyModel.objects.select_related('created_user').all()
This query working fine, but if the user deleted from the database, then it is showing empty queryset .此查询工作正常,但如果用户从数据库中删除,则显示为空查询集。
If you want the instance to be protected after deletion then you can use models.PROTECT
option so:如果您希望实例在删除后受到保护,那么您可以使用
models.PROTECT
选项:
class MyModel(models.Model):
created_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
See all the options of ForeignKey.on_delete
.查看
ForeignKey.on_delete
的所有选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.