Slug — it is essentially a short label for something, containing only letters, numbers, underscores, or hyphens. They are commonly used in URLs. For example, in a typical blog post URL:
Here the last
add-the-slug-field-inside-django-model is a slug.
SlugField in Django is like CharField where you also you can specify the
max_length attribute. If max_length is not specified, Django will use the default length of 50. This also assumes that Field.db_index is set to True . It is often useful to automatically prefill a SlugField based on a different value. For validation use validate_slug or validate_unicode_slug. ,
field_name = models.SlugField (max_length = 200, ** options )
SlugField has the following optional arguments:
The maximum length (in characters) of the field. Maximum
max_length applies at the database level and when validating Django using MaxLengthValidator .
If True, the field accepts Unicode letters in addition to ASCII letters. Default False.
Django SlugField Model Explanation
Illustration of a SlugField using an example. Consider a project named
pythonengineering that has an application named
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into the
models.py file of the geeks application.
Add the geek app to
Now when we run
makemigrations from the terminal,
Python manage.py makemigrations
A new folder named migrations will be created in the
geeks directory with a file named
Python manage.py migrate
geeks_field SlugField is generated when migrations are run in the project.
How to use SlugField?
SlugField is used to store mostly URL paths after a specific URL. To learn more about how to properly add a SlugField to your Django Project, refer to this article — Now let’s check it out on administrator a running server. We have created a GeeksModel instance. Field options — they are arguments given to each field to apply some constraint or to convey a particular characteristic to a particular field. For example, adding the
# import the model
# from the geek app
GeeksModel. objects.create (geeks_field
null = True argument to the SlugField will allow it to store empty values for that table in a relational database.
Here are the field options and attributes that SlugField can use.
Now let’s check it out on administrator a running server. We have created a GeeksModel instance.
Field options — they are arguments given to each field to apply some constraint or to convey a particular characteristic to a particular field. For example, adding the
|Null||If True , Django will store empty values as NULL in the database. Default is False.|
|Blank||If True , the field is allowed to be blank ... Default is False.|
|Choices||An iterable (eg, a list or tuple) of 2-tuples to use as choices for this field.|
|Default||The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.|
|help_text||Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.|
|primary_key||If True, this field is the primary key for the model .|
|Unique||If True, this field must be unique throughout the table.|