Skip to content

Searchable=True field sometimes works, most of the time does not. #47

@jsandovalcardona

Description

@jsandovalcardona

I have the following code:

class ApiKey(models.Model):
    tenant = models.ForeignKey(Tenant, null=False, on_delete=models.CASCADE)
    key = secured_fields.EncryptedCharField(
        max_length=constants.CHARFIELD_SIZE_128, unique=True, editable=False, searchable=True
    )

    def save(self, *args, **kwargs):
        if not self.key:
            self.key = secrets.token_urlsafe(64)
        super().save(*args, **kwargs)

And the following code:

    api_key = request.headers.get("x-api-key")
    print("api_key", api_key)
    print(ApiKey.objects.first().key == api_key)  # This returns True
    provider_api_key = ApiKey.objects.get(key=api_key)  # This fails, most of the time, with self.model.DoesNotExist. But, sometimes it works!!

Am I doing something wrong? I set the field to searchable. I was thinking with that would be enough to filter and get by it. But, it's not working for me. Why does it works sometimes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions