[Django] #32207: Slug field is accepting whitespace when entered through Django shell.

12 views
Skip to first unread message

Django

unread,
Nov 18, 2020, 12:27:23 PM11/18/20
to django-...@googlegroups.com
#32207: Slug field is accepting whitespace when entered through Django shell.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
Harshvardhan Anand |
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) | Keywords: slug; prepopulated;
Severity: Normal | error reporting; sqlite
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I am using **Django 3.1.3** with **Python 3.7.6**.

1. Slug field is **accepting whitespaces** when updated from Django shell.
2. Slug field is **not prepopulated** if data(like title) is entered from
Django shell.

But if slug is entered with whitespaces from admin panel then it will
raise error. And similarly if we create data(e.g. - title) from admin
panel slug field is prepopulated.

[https://bit.ly/2Km19CX I have also created a video(saved in google
drive) explaining the issue.]

**In the image below, you can see the slug field with whitespaces -**

[[Image(https://bit.ly/35FB7Te)]]

**The model I have defined in models.py is**

{{{
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published')
)

title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='blog_post')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES,
default='draft')

class Meta:
ordering = ('-publish',)

def __str__(self):
return self.title
}}}


**Modified admin panel -**

{{{
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish', 'status')
list_filter = ('status', 'created', 'publish', 'author')
search_fields = ('title', 'body')
prepopulated_fields = {'slug':('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ('status', 'publish')
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32207>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 18, 2020, 12:28:08 PM11/18/20
to django-...@googlegroups.com
#32207: Slug field is accepting whitespace when entered through Django shell.
-------------------------------------+-------------------------------------
Reporter: Harshvardhan Anand | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: slug; prepopulated; | Triage Stage:
error reporting; sqlite | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Harshvardhan Anand):

* Attachment "db.png" added.

Django

unread,
Nov 18, 2020, 1:16:43 PM11/18/20
to django-...@googlegroups.com
#32207: Slug field is accepting whitespace when entered through Django shell.
-------------------------------------+-------------------------------------
Reporter: Harshvardhan Anand | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: slug; prepopulated; | Triage Stage:
error reporting; sqlite | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* status: new => closed
* resolution: => invalid


Comment:

This is expected behavior. The `SlugField` functionality is implemented
though validation, which is activated by the forms infrastructure or by
manually calling `full_clean()` on some model. If you set a value and
save, you are on your own, there are no database-level checks. You can see
the exact same behaviour for other fields like `URLField`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32207#comment:1>

Reply all
Reply to author
Forward
0 new messages