英文:
Django SELECT Query
问题 {#heading}
以下是您要翻译的内容:
我正在尝试使用Django从mysql中进行选择
我的**models.py**
from django.db import models
from django.db.models import Q
class Series(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=255)
des = models.CharField(max_length=255)
img = models.CharField(max_length=255)
isseries = models.IntegerField()
istrend = models.CharField(max_length=255)
background_img = models.CharField(max_length=255)
trial_url = models.CharField(max_length=255)
imdb_id = models.CharField(max_length=255)
rate = models.CharField(max_length=255)
date = models.CharField(max_length=255)
status = models.IntegerField()
isdel = models.IntegerField()
section_id = models.IntegerField()
poster_img = models.CharField(max_length=255)
genres = models.CharField(max_length=255)
class Meta:
db_table = 'series'
我的示例脚本
series = series_db.objects.filter(istrend=1,status=1,isdel=0).order_by('id').values()
我想选择所有**列**,但不包括isdel
所以我从**models.py**中删除了isdel,但当我删除它后,我无法使用isdel来创建
WHERE isdel = 0
在mysql中,所以我收到了这个错误
django.core.exceptions.FieldError: 无法解析关键字'isdel'为字段。
那么我如何使用isdel来创建条件而不选择它
就像这个例子一样
SELECT `series`.`id`, `series`.`title`, `series`.`des`, `series`.`img`, `series`.`isseries`, `series`.`istrend`, `series`.`background_img`, `series`.`trial_url`, `series`.`imdb_id`, `series`.`rate`, `series`.`date`, `series`.`status`, `series`.`section_id`, `series`.`poster_img`, `series`.`genres` FROM `series` WHERE (`series`.`isdel` = 0 AND `series`.`istrend` = '1' AND `series`.`status` = 1) ORDER BY `series`.`id` ASC
真实示例
SELECT `series`.`id`, `series`.`title`, `series`.`des`, `series`.`img`, `series`.`isseries`, `series`.`istrend`, `series`.`background_img`, `series`.`trial_url`, `series`.`imdb_id`, `series`.`rate`, `series`.`date`, `series`.`status`, `series`.`isdel`, `series`.`section_id`, `series`.`poster_img`, `series`.`genres` FROM `series` WHERE (`series`.`isdel` = 0 AND `series`.`istrend` = '1' AND `series`.`status` = 1) ORDER BY `series`.`id` ASC
英文:
Hi Iam try to select from mysql using Django
my models.py
from django.db import models
from django.db.models import Q
class Series(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=255)
des = models.CharField(max_length=255)
img = models.CharField(max_length=255)
isseries = models.IntegerField()
istrend = models.CharField(max_length=255)
background_img = models.CharField(max_length=255)
trial_url = models.CharField(max_length=255)
imdb_id = models.CharField(max_length=255)
rate = models.CharField(max_length=255)
date = models.CharField(max_length=255)
status = models.IntegerField()
isdel = models.IntegerField()
section_id = models.IntegerField()
poster_img = models.CharField(max_length=255)
genres = models.CharField(max_length=255)
class Meta:
db_table = 'series'
my sample Script
series = series_db.objects.filter(istrend=1,status=1,isdel=0).order_by('id').values()
i Want to SELECT all Column without isdel
so i remove isdel from models.py , but when i remove it i cant use isdel to make
WHERE isdel = 0
in mysql so i got This Error
django.core.exceptions.FieldError: Cannot resolve keyword 'isdel' into field.
so how i can use isdel to make a condition without select it
LIKE this Example
SELECT `series`.`id`, `series`.`title`, `series`.`des`, `series`.`img`, `series`.`isseries`, `series`.`istrend`, `series`.`background_img`, `series`.`trial_url`, `series`.`imdb_id`, `series`.`rate`, `series`.`date`, `series`.`status`, `series`.`section_id`, `series`.`poster_img`, `series`.`genres` FROM `series` WHERE (`series`.`isdel` = 0 AND `series`.`istrend` = '1' AND `series`.`status` = 1) ORDER BY `series`.`id` ASC
real Example
SELECT `series`.`id`, `series`.`title`, `series`.`des`, `series`.`img`, `series`.`isseries`, `series`.`istrend`, `series`.`background_img`, `series`.`trial_url`, `series`.`imdb_id`, `series`.`rate`, `series`.`date`, `series`.`status`, `series`.`isdel`, `series`.`section_id`, `series`.`poster_img`, `series`.`genres` FROM `series` WHERE (`series`.`isdel` = 0 AND `series`.`istrend` = '1' AND `series`.`status` = 1) ORDER BY `series`.`id` ASC
答案1 {#1}
得分: 2
你不需要在模型中删除字段,只需在values
中提到要检索的列,就像这样:
series = Series.objects.filter(
istrend=1, status=1, isdel=0
).order_by('id').values('id', 'title', 'des')
我只添加了三列到values
中,你可以添加除了你不想要的列之外的所有列。
如果你的模型有很多字段,手动一个一个添加它们会很困难,那么你可以使用_meta
获取模型的所有字段,并排除指定的字段,就像这样:
fields = [field.name for field in Series._meta.fields if field.name != 'isdel']
series = Series.objects.filter(
istrend=1, status=1, isdel=0
).order_by('id').values(*fields)
英文:
You don't have to remove the field in model just mention the column you want to retrive in values
like this:
series = Series.objects.filter(
istrend=1,status=1,isdel=0
).order_by('id').values('id', 'title', 'des')
I have added just three columns in values you can add all the columns except the one you don't want.
If your model has lot of field and you find it hard to add them manually one by one then you can get all the fields of the model using _meta
and exclude the given field like this:
fields = [field.name for field in Series._meta.fields if field.name != 'isdel']
series = Series.objects.filter(
istrend=1,status=1,isdel=0
).order_by('id').values(*fields)
答案2 {#2}
得分: 2
你可以使用values。所以你的查询将被修改为:
series = series_db.objects.filter(istrend=1, status=1, isdel=0).order_by('id').values('id')
英文:
You can use values
So your query would be modified to:
series = series_db.objects.filter(istrend=1,status=1,isdel=0).order_by('id').values("id")
答案3 {#3}
得分: 1
似乎您遇到了一个问题,因为您从您的"Series"模型中删除了"isdel"字段,但仍然希望在筛选条件中使用它。
以下是如何修改您的示例脚本以实现您想要的查询:
from django.db.models import Q
series = Series.objects.filter(
Q(istrend=1) & Q(status=1)
).extra(where=["`series`.`isdel` = 0"]).order_by('id').values()
在这个修改后的脚本中,我使用了"extra"方法来向您的查询添加自定义的"where"条件,指定了"isdel"的条件,而不需要它作为模型中的字段。其余的筛选条件保持不变。 英文:
It seems like you're encountering an issue because you've removed the isdel
field from your Series
model, but you still want to use it in your filtering conditions.
Here's how you can modify your sample script to achieve your desired query:
from django.db.models import Q
series = Series.objects.filter(
Q(istrend=1) & Q(status=1)
).extra(where=["`series`.`isdel` = 0"]).order_by('id').values()
In this modified script, I'm using the extra
method to add a custom where
condition to your query, specifying the condition for isdel
without having it as a field in the model. The rest of your filtering conditions remain the same.