Code

Opened 7 months ago

Closed 6 months ago

#21156 closed New feature (needsinfo)

Inline model admin does not respect model field default values.

Reported by: tom.vaughan@… Owned by: nobody
Component: contrib.admin Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given the abbreviated code below, in the admin view for Project Proposals there will be three (the default value of "extra") new Resubmit Project Proposal Links each with their own unique resubmit_id, and there will be a link to "+ Add another Resubmit Project Proposal Link". This works the first time it's clicked. That is the new (fourth) Resubmit Project Proposal Link will have a unique (distinct from the first three at least) resubmit_id, let's say 1234. But each time the link is clicked afterwards the resubmit_id in the newly created Resubmit Project Proposal Link will always be 1234.

def _default_resubmit_id():
    return uuid.uuid4().hex

class ResubmitProjectProposalLink(models.Model):                                                                                                                   
    resubmit_id = fields.RequiredCharField(default=_default_resubmit_id)
    projectproposal = fields.RequiredForeignKey('ProjectProposal', related_name='resubmit_projectproposal_links')

class ResubmitProjectProposalLinkInline(admin.StackedInline):
    model = ResubmitProjectProposalLink

class ProjectProposalAdmin(admin.ModelAdmin):
    inlines = [ResubmitProjectProposalLinkInline]

Attachments (0)

Change History (2)

comment:1 Changed 7 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Do you have a proposal for how to address this? It seems to be like it would be prohibitively difficult to address in a general fashion as it would require doing something like detecting that the default value should be dynamically generated and then providing an API that could be used to fetch new values when clicking "Add Another". I think you are better off customizing the admin yourself if you need this functionality.

comment:2 Changed 6 months ago by timo

  • Component changed from Uncategorized to contrib.admin
  • Resolution set to needsinfo
  • Status changed from new to closed
  • Type changed from Uncategorized to New feature

Closing as needs info/won't fix in absence of a proposal for how to address this.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.