Home > Net >  Django error ValueError: Field 'id' expected a number but got ''
Django error ValueError: Field 'id' expected a number but got ''

Time:10-12

I create django model for keep api_key as char like this.

class DeviceKey(models.Model):
    
    api_key=models.CharField(max_length=100,unique=True)
    limit_device=models.IntegerField()
    created=models.DateTimeField(auto_now_add=True)
    updated=models.DateTimeField(auto_now=True)

    
    def __str__(self):
        return self.api_key
    

    class Meta:
        ordering=('api_key',)
        verbose_name='DEVICE KEY'
        verbose_name_plural='DEVICE KEY'
        
class Device(models.Model):
    api_key=models.ForeignKey(DeviceKey,on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    status=models.BooleanField()
    switch=models.BooleanField(default=False)
    timer=models.BooleanField(default=False)
    category=models.ForeignKey(Category,on_delete=models.CASCADE)
    created=models.DateTimeField(auto_now_add=True)
    updated=models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering=('api_key',)

I create function device() in views.py for get data by using api_key like this.

def device(request):

    key = request.GET.getlist('key')
    data=Device.objects.filter(api_key__in=key).values_list('name','status')
    
    return JsonResponse(list(data), safe=False)
   

In admin page I create sample api_key as 2eaWe. when I run and send api_key to function device() it show error like this traceback.

Traceback (most recent call last):
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
        response = get_response(request)
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/home/it/myproject/iotmy/device/views.py", line 20, in device
        data=Device.objects.filter(api_key__in=key).values_list('name','status')
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/query.py", line 1071, in filter
        return self._filter_or_exclude(False, args, kwargs)
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/query.py", line 1089, in _filter_or_exclude
        clone._filter_or_exclude_inplace(negate, args, kwargs)
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/query.py", line 1096, in _filter_or_exclude_inplace
        self._query.add_q(Q(*args, **kwargs))
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1466, in add_q
        clause, _ = self._add_q(q_object, self.used_aliases)
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1496, in _add_q
        child_clause, needed_inner = self.build_filter(
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1412, in build_filter
        condition = self.build_lookup(lookups, col, value)
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1242, in build_lookup
        lookup = lookup_class(lhs, rhs)
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/lookups.py", line 27, in __init__
        self.rhs = self.get_prep_lookup()
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/fields/related_lookups.py", line 77, in get_prep_lookup
        self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/fields/related_lookups.py", line 77, in <listcomp>
        self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
      File "/home/it/myproject/myproject_env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1990, in get_prep_value
        raise e.__class__(
    ValueError: Field 'id' expected a number but got '2eaWe'.

How to fix error ValueError: Field 'id' expected a number ?

CodePudding user response:

data=Device.objects.filter(api_key__in=key).values_list('name','status')

that mean you will search in api_key model using id

if you want to search by api_key the query should be

data=Device.objects.filter(api_key__api_key__in=key).values_list('name','status')
  • Related