Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django admin panel shows field max length instead of field name

I know it is a little bit weird but I see field max length instead of field name in admin panel as below:

enter image description here

My model:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class SituationFlag(models.Model):
name=models.CharField(30)
slug=models.SlugField(null=False,blank=True, unique=True,editable=False,max_length=30)
description =models.CharField(200)
cssclass=models.CharField(30)

def __str__(self) -> str:
    return self.name
def save(self,*args,**kwargs):
    self.slug=slugify(self.name)
    super().save(*args,**kwargs)

Also I’m using that SituationFlag model with many-many relationship in other model as below:

class Subject(models.Model):
title=models.CharField(max_length=200)
description = models.TextField()
is_active=models.BooleanField(default=True)
slug=models.SlugField(null=False,blank=True, unique=True,db_index=True,editable=False,max_length=255)
category=models.ForeignKey(Category,on_delete= models.SET_NULL,null=True)
situation_flag=models.ManyToManyField(SituationFlag)

def __str__(self) -> str:
    return self.title

def save(self,*args,**kwargs):
    self.slug=slugify(self.title)
    super().save(*args,**kwargs)

What am I missing here?

Any help would be much appreciated.

>Solution :

You did not pass these to the max_length=… [Django-doc], but to the verbose_name=… [Django-doc]. Use a named parameter:

class SituationFlag(models.Model):
    name = models.CharField(max_length=30)
    slug = models.SlugField(
        null=False, blank=True, unique=True, editable=False, max_length=30
    )
    description = models.CharField(max_length=200)
    cssclass = models.CharField(max_length=30)

    def __str__(self) -> str:
        return self.name

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super().save(*args, **kwargs)

Note: Specifying null=False [Django-doc] is not necessary: fields are by default not NULLable.


Note: You can make use of django-autoslug [GitHub] to automatically create a slug based on other field(s).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading