1

I want to query the item from the database using django SmallIntegerField. The code I used is

Here is the class defination:

class Equipment(models.Model):
    asset_status = (
        (0, 'online'),
        (1, 'offline'),
        (2, 'unknown'),
        (3, 'wrong'),
        (4, 'x'),
    )
status = models.SmallIntegerField(
        choices=asset_status, default=0, verbose_name='Device Status')

The query code I used is

def filter_queryset(self, qs):
    sSearch = self.request.GET.get('search[value]', None)
    print(sSearch)
    if sSearch:
        qs = qs.filter(Q(status__icontains=sSearch))
        return qs

I want to query this column by 'online', 'offline' and so on. Do you know how to do this?

The reference I have searched are

Search choice field by the name in django - python

I also see the Choices API in https://django-model-utils.readthedocs.io/en/latest/utilities.html#choices

But there is no Q. I am not sure whether it works. Could you please tell me about this?

And I searched for a method called get_FOO_display, but I don't think it can be solved. Neither did I think it can be combined with Q.

Thanks

Elegant Lin
  • 25
  • 2
  • 10

2 Answers2

2

Choices follow the format of a actual_value_for_db, human_readable_name tuple, where the human_readable_name is only for display purposes. That is, you cannot query for it as only the first item of the tuple is stored in the database.

If you want to make it a bit easier to find out what value you need to use you can use an enum-like declaration of the choices:

class Equipment(models.Model):
    ONLINE = 0
    OFFLINE = 1
    UNKNOWN = 2
    WRONG = 3
    X = 4

    asset_status = (
        (ONLINE, 'online'),
        (OFFLINE, 'offline'),
        (UNKNOWN, 'unknown'),
        (WRONG, 'wrong'),
        (X, 'x'),
    )

Then you can query for a choice by using a 'name' of the member of the Equipment model: Equipment.objects.filter(status=Equipment.OFFLINE)

It is not clear what exactly the value in your request is and where it comes from. I would suggest just using a ModelForm to ask the user for the right status type. A ModelForm should take care of the choices for that formfield and return the right type for the selected choice (which should be an integer, not a string!). Use a generic FormView and the rest should be a piece of cake.

CoffeeBasedLifeform
  • 2,296
  • 12
  • 27
0

status is a SmallIntegerField so, what you have in the database are integers. When you provide code like this:

asset_status = (
    (0, 'online'),
    (1, 'offline'),
    (2, 'unknown'),
    (3, 'wrong'),
    (4, 'x'),
)

you're telling Django: "hey, I want to be able to display the 0 option value as 'online' , 1 as 'offline' ..." and so on.

So, you Django don't write such values ('online', 'offline', ...) to the database hence you can't query on them.

But ...

I suppose you are having trouble getting the right integer value for the query after the user selects one of the options for filtering, you should take a look at ChoiceField, perhaps you can use it for your filter form.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60