📜  django admin.py 所有字段 - Python (1)

📅  最后修改于: 2023-12-03 15:30:28.419000             🧑  作者: Mango

Django Admin Fields

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.

BooleanField

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()
CharField

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)
TextField

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()
DateField

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()
DateTimeField

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()
DecimalField

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)
EmailField

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()
FileField

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()
FloatField

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()
ImageField

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()
IntegerField

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()
PositiveIntegerField

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()
SmallIntegerField

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()
SlugField

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()
TimeField

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()
URLField

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()
ForeignKey

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)
ManyToManyField

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)
OneToOneField

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)
Conclusion

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.