49👍
class User(models.Model):
fname = models.CharField(max_length=50, verbose_name = 'first name')
class Meta:
verbose_name = "users"
Source: https://docs.djangoproject.com/en/2.1/topics/db/models/#meta-options
18👍
verbose_name and verbose_name_plural both the properties of Meta class are very important to modify the default behaviour of Django to display the name of our models on Admin interface.
You can change the display of your model names using on Admin Interface using verbose_name and verbose_name_plural properties and model fields names using keyword argument verbose_name.
Please find the below 2 examples.
Country model:
class Country(models.Model):
name = models.CharField(max_length=100, null=False, blank=False, help_text="Your country", verbose_name="name")
userid = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f"Country {str(self.id)} - {self.name}"
class Meta:
verbose_name = "Country"
verbose_name_plural = "Countries"
If you will not specify verbose_name_plural then Django will take it as Countrys which does not look good as we want it as Countries.
This better fits in the following type of Model.
Gender model:
class Gender(models.Model):
name = models.CharField(max_length=100, null=False, blank=False, help_text="Gender", verbose_name = "name")
userid = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f"Gender {str(self.id)} - {self.name}"
class Meta:
verbose_name = "Gender"
- [Django]-Folder Structure for Python Django-REST-framework and Angularjs
- [Django]-AUTH_USER_MODEL refers to model .. that has not been installed and created AbstractUser models not able to login
- [Django]-Drawbacks to running Django under PyPy?
- [Django]-Django – Get Foreign key objects in single query?
- [Django]-Django TypeError: get() got multiple values for keyword argument 'invoice_id'
- [Django]-No module named django but it is installed
6👍
To alter admin model without polluting the model itself, you can utilize a proxy admin model, like this:
# admin.py
from . import models
class Users(models.User):
class Meta:
proxy = True
class UsersAdmin(admin.ModelAdmin):
...
admin.site.register(Users, UsersAdmin)
- [Django]-What is reverse()?
- [Django]-Handlebars.js in Django templates
- [Django]-Django model with 2 foreign keys from the same table
0👍
ConfigAbility._meta.verbose_name = ‘config ability’
ConfigAbility._meta.verbose_name_plural = ‘config ability’
I did explore this, but don’t know whether it’s the thing you need. I put those codes in class ConfigAbilityAdmin in Admin.py. Then, the result:
With this approach, you don’t need to config Meta method in model class, especially when model class’s code is generated from inspectdb…
- [Django]-Django – query filter on manytomany is empty
- [Django]-How to remove white space from html source code
- [Django]-Can I make an admin field not required in Django without creating a form?