📅  最后修改于: 2023-12-03 15:30:28.419000             🧑  作者: Mango
The Django Admin is a powerful tool for managing your application's data. It provides an interface for performing CRUD (Create, Read, Update, Delete) operations on your database models. Here, we will cover all the fields that can be used in Django Admin.
The BooleanField is used to represent a boolean value, either True or False. It is displayed as a checkbox in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.BooleanField()
The CharField is used to store character data. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.CharField(max_length=255)
The TextField is used to store a large amount of text. It is displayed as a multi-line text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.TextField()
The DateField is used to store a date. It is displayed as a date picker in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.DateField()
The DateTimeField is used to store a date and time. It is displayed as a date-time picker in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.DateTimeField()
The DecimalField is used to store decimal values. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.DecimalField(max_digits=5, decimal_places=2)
The EmailField is used to store an email address. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.EmailField()
The FileField is used to store a file. It is displayed as a file upload field in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.FileField()
The FloatField is used to store floating point numbers. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.FloatField()
The ImageField is used to store an image. It is displayed as an image upload field in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.ImageField()
The IntegerField is used to store integer values. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.IntegerField()
The PositiveIntegerField is used to store positive integer values. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.PositiveIntegerField()
The SmallIntegerField is used to store small integer values. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.SmallIntegerField()
The SlugField is used to store a slug, which is a short label for something, containing only ASCII letters, numbers, hyphens, or underscores. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.SlugField()
The TimeField is used to store a time. It is displayed as a time picker in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.TimeField()
The URLField is used to store a URL. It is displayed as a text input box in the Django Admin.
from django.db import models
class MyModel(models.Model):
field_name = models.URLField()
The ForeignKey is used to represent a many-to-one relationship between two database models. It is displayed as a drop-down list in the Django Admin.
from django.db import models
class RelatedModel(models.Model):
field_name = models.CharField(max_length=255)
class MyModel(models.Model):
related_field = models.ForeignKey(RelatedModel, on_delete=models.CASCADE)
The ManyToManyField is used to represent a many-to-many relationship between two database models. It is displayed as a list of checkboxes in the Django Admin.
from django.db import models
class RelatedModel(models.Model):
field_name = models.CharField(max_length=255)
class MyModel(models.Model):
related_field = models.ManyToManyField(RelatedModel)
The OneToOneField is used to represent a one-to-one relationship between two database models. It is displayed as a drop-down list in the Django Admin.
from django.db import models
class RelatedModel(models.Model):
field_name = models.CharField(max_length=255)
class MyModel(models.Model):
related_field = models.OneToOneField(RelatedModel, on_delete=models.CASCADE)
These are all the fields that can be used in Django Admin. As you can see, Django provides a wide range of fields to choose from for your data models.