#14095: Objects not saved when readonly_fields is set for inline admin
-------------------------------------------+--------------------------------
          Reporter:  si...@pewpewlabs.com  |         Owner:  nobody             
                                  
            Status:  closed                |     Milestone:                     
                                  
         Component:  django.contrib.admin  |       Version:  1.2                
                                  
        Resolution:  invalid               |      Keywords:  TabularInline, 
inline, readonly_fields, sprintdec2010
             Stage:  Unreviewed            |     Has_patch:  0                  
                                  
        Needs_docs:  0                     |   Needs_tests:  0                  
                                  
Needs_better_patch:  0                     |  
-------------------------------------------+--------------------------------
Changes (by julien):

  * keywords:  TabularInline, inline, readonly_fields => TabularInline,
               inline, readonly_fields, sprintdec2010
  * status:  new => closed
  * resolution:  => invalid

Comment:

 Thank you for the report, Simon. It is true that Django is behaving oddly
 here. However, this is caused by the way inlines are currently designed to
 work. If you make the `code` field readonly, then there will not be any
 input added to the form for it, which means that Django has no way of
 detecting that anything has changed in the form, and therefore cannot know
 that an object should be created.

 I've discussed this at length with russelm and DrMeers (both core
 committers), and the answer is that this cannot be fixed. To achieve what
 you want you'd probably have to not make the `code` field readonly, and
 then use a bit of javascript to prevent the user from modifying it.

 For that reason I'm closing this ticket as invalid. I also invite you to
 look into ticket #14832, which has in fact emerged after looking at this
 one and which discusses a related issue.

 Finally, for the record, here's the code I've used to study this issue
 (some elements like `generate_balance_code` or `SERIES_CODE_COUNT` were
 missing in your description):

 Models:
 {{{
 #!python

 import datetime, hashlib

 from django.db import models
 from django.contrib.auth.models import User

 def generate_balance_code():
     return hashlib.md5(str(datetime.datetime.now())).hexdigest()


 class RefillSeries(models.Model):
     issued = models.DateField(default=datetime.datetime.today)
     least_valid_until = models.DateField(default=datetime.datetime.today()
 + datetime.timedelta(days=10000))

 class BalanceCode(models.Model):
     created_at = models.DateField(auto_now_add=True)
     code = models.CharField(max_length=100, unique=True,
 default=generate_balance_code,)
     value = models.PositiveIntegerField(default=10)
     refill_series = models.ForeignKey(RefillSeries)
     used_by = models.ForeignKey(User, null=True, blank=True)
     used_at = models.DateField(blank=True, null=True)
 }}}

 Admin:
 {{{
 #!python

 from django.contrib import admin

 from .models import BalanceCode, RefillSeries

 class BalanceCodeAdmin(admin.ModelAdmin):
     readonly_fields = ('code', )

 class BalanceCodeInline(admin.TabularInline):
     readonly_fields = ('code', )
     model = BalanceCode
     extra = 2
     max_num = 5

 class RefillSeriesAdmin(admin.ModelAdmin):
     inlines = [BalanceCodeInline]

 admin.site.register(BalanceCode, BalanceCodeAdmin)
 admin.site.register(RefillSeries, RefillSeriesAdmin)
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14095#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to