CITextField in admin is displayed as text area, not as one line

Viewed 22

I'm using python 3.9 with django 4.1

I'm just starting to play with it, I want to use CITextField from Postgresql so i imported it from django.contrib.postgres.fields, it works fine but that admin displays it as TextArea, which means it's not a one-liner input text, it's a all box of text to enter even if i configured a max length of 100 characters. how can I correct that?

as an example to show the problem I created a Country model with name_en which is a CharField and name_fr and name_he which are CITextField, all of them are set to max length of 100 characters:

class Country(models.Model):
    class Meta:
        verbose_name_plural = "Countries"
    name_en = models.CharField(unique=True, verbose_name="English Country Name", max_length=100)
    name_fr = CITextField(unique=True, verbose_name="French Country Name", max_length=100)
    name_he = CITextField(unique=True, verbose_name="Hebrew Country Name", max_length=100)

when i browse it on the admin page this is the result:

admin page of country model

what can I do to resolve it ?

thanks

1 Answers

I can create my own model class and use it to return a TextInput instead of FormField, I don't really like that answer, I notice that CharField width is longer than my custom textField so it doesn't act exactly the same.

So this is MyCITextField that I defined:

class MyCITextField(CITextField):
    def formfield(self, **kwargs):
        kwargs.update({"widget": forms.TextInput})
        return super(MyCITextField, self).formfield(**kwargs)

And while having this as my model:

class Country(models.Model):
    class Meta:
        verbose_name_plural = "Countries"
    name_en = MyCITextField(unique=True, verbose_name="English Country Name", max_length=100)
    name_fr = models.CharField(unique=True, verbose_name="French Country Name", max_length=100)
    name_he = models.CharField(unique=True, verbose_name="Hebrew Country Name", max_length=100)

the result is:

MyCITextField used

so as you can see that don't share the same with, i want the visuality to be the same.

Related