Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#28205 closed Cleanup/optimization (fixed)

Document that ModelAdmin.prepopulated_fields only works on empty forms

Reported by: Ju Owned by: Botond Béres
Component: Documentation Version: 1.11
Severity: Normal Keywords: prepopulated_fields
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

ModelAdmin.prepopulated_fields auto generating works only while creating new records but not while editing them.

from django.db import models

class Item(models.Model):
    title = models.CharField(max_length=200, blank=False)
    slug = models.SlugField(max_length=200)

from django.contrib import admin
from .models import Item

class ItemAdmin(admin.ModelAdmin):
   
  prepopulated_fields = {"slug": ("title",)}

admin.site.register(Item, ItemAdmin)

Change History (8)

comment:1 Changed 5 years ago by Tim Graham

Component: contrib.adminDocumentation
Summary: ModelAdmin.prepopulated_fields. Auto generating does not work while editing objectDocument that ModelAdmin.prepopulated_fields only works on empty forms
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

That's expected behavior -- prepopulated_fields only works on empty forms. I think the idea is that slugs for existing objects shouldn't change since that could cause a URL to change. This could be documented.

comment:2 Changed 5 years ago by James Seden Smith

Owner: changed from nobody to James Seden Smith
Status: newassigned

comment:3 Changed 5 years ago by Botond Béres

Owner: changed from James Seden Smith to Botond Béres

comment:4 Changed 5 years ago by Botond Béres

Has patch: set

Opened a small PR which adds in the prepopulated_fields docs that it only works on empty forms.

comment:5 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 56b364ba:

Fixed #28205 -- Doc'd that ModelAdmin.prepopulated_fields only works on add forms.

comment:6 Changed 5 years ago by Tim Graham <timograham@…>

In 5630f4e6:

[2.0.x] Fixed #28205 -- Doc'd that ModelAdmin.prepopulated_fields only works on add forms.

Backport of 56b364bacca0d6a6cd8fbcd93aeb49f3ad46e58c from master

comment:7 Changed 4 years ago by Tim Graham <timograham@…>

In 682cdf6c:

Refs #28205 -- Corrected ModelAdmin.prepopulated_fields docs regarding when they're populated.

comment:8 Changed 4 years ago by Tim Graham <timograham@…>

In a9d9680e:

[2.1.x] Refs #28205 -- Corrected ModelAdmin.prepopulated_fields docs regarding when they're populated.

Backport of 682cdf6cab8cb76ef1808df45631c39748052e13 from master.

Note: See TracTickets for help on using tickets.
Back to Top