Store phone field use django-phonenumber-field override search method to e164 format

i have store phone field to database using django-phonenumber-field with e164 format, its success formated and succesfully.

this is my

class External(models.Model):
name = models.CharField(max_length=255, unique=True)
address = models.CharField(max_length=255)
type= models.ForeignKey(Type, on_delete=models.CASCADE, verbose_name="Type")
region= models.ForeignKey(Region, on_delete=models.CASCADE, verbose_name="Region")
email = models.EmailField(max_length=254, unique=True)
phone = PhoneNumberField(null=True, blank=True, unique=True)
status = models.BooleanField(default=True)
created_at = models.DateTimeField('date created', auto_now_add=True)
updated_at = models.DateTimeField('last edited', auto_now=True)

def __str__(self):

but the problem is when i seacrh using national format like 0216333007 no data found. can someone help me to override the search methode when user input 0216333007 auto format to e164 = +62216333007 ?

this is my

 exclude = ()
form = ExternalAdminForm
list_per_page = 10
readonly_fields = ('created_at', 'updated_at')
list_display = ('name', 'type_id_slug', 'short_address', 'region_id_slug', 'email_link', 'phone', 'status', 'created_at', 'updated_at')
list_filter = (StatusFilter, TypeFilter, RegionFilter,)
search_fields = ('name', 'address', 'email', 'phone',)

im using django 4.1.5

thanks to anyone who helps me. sorry for my bad english.

i found the answer

search_fields = ('name', 'address', 'email', 'phone',)

def get_search_results(self, request, queryset, search_term):
    queryset, use_distinct = super().get_search_results(request, queryset, search_term)
        search_term = PhoneNumber.from_string(search_term, None).as_e164
        queryset |= self.model.objects.filter(phone=search_term)
    return queryset, use_distinct
Back to Top