0

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
Ghost Ghaith
  • 29
  • 1
  • 6

3 Answers3

2

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)
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
2

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")
1

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.