#28831: InlineModelAdmin.get_fieldsets should receive own object, not parent
-----------------------------------------+------------------------
               Reporter:  tonnzor        |          Owner:  nobody
                   Type:  Bug            |         Status:  new
              Component:  contrib.admin  |        Version:  2.0
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 When I use `ModelAdmin.get_fieldsets`, I receive current object. In my
 example that would be -- when I call `AccountAdmin.get_fieldsets` -- I
 receive `Account`. That enable me to generate different field sets for
 different cases.

 BUT when I use `InlineModelAdmin.get_fieldsets`, I receive parent object.
 In my example that would be -- when I call
 `AccountInline.get_fieldsets` -- I receive `Customer` object instead of
 `Account. That prevents me from configuring formsets to the account I
 have.

 If I received `Account`, I could extract `Customer` from it. But if I
 receive `Customer`, I cannot adapt form for each row.

 Also, it seems strange that class explicitly linked to `Account` receives
 `Customer` object as input.

 Code:

 {{{
 # in models.py

 class Customer(models.Model):
     num = models.IntegerField()


 class Account(models.Model):
     ACCOUNT_TYPES = (
         (1, 'A'),
         (2, 'B'),
     )
     customer = models.ForeignKey(Customer)
     account_type = models.IntegerField(choices=ACCOUNT_TYPES)

     a = models.CharField(max_length=255, blank=True) # should be edited
 when type = "A"
     b = models.CharField(max_length=255, blank=True) # should be edited
 when type = "B"

 # in admin.py

 class AccountInline(admin.TabularInline):
     model = models.Account

     def get_fieldsets(self, request, obj=None):
         print(repr(obj)) # => <Customer>
         """
         if obj and obj.account_type == 1:
             return ((None, {'fields': ('account_type', 'a')}),)
         elif obj and obj.account_type == 2:
             return ((None, {'fields': ('account_type', 'b')}),)
         """
         return ((None, {'fields': ('account_type',)}),)

 @admin.register(models.Customer)
 class CustomerAdmin(admin.ModelAdmin):
     inlines = (AccountInline,)

 @admin.register(models.Account)
 class AccountAdmin(admin.ModelAdmin):
     def get_fieldsets(self, request, obj=None):
         print(repr(obj)) # => <Account>
         if obj and obj.account_type == 1:
             return ((None, {'fields': ('account_type', 'a')}),)
         elif obj and obj.account_type == 2:
             return ((None, {'fields': ('account_type', 'b')}),)
         return ((None, {'fields': ('account_type',)}),)

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28831>
Django <https://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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.74b89cdf72bcde97196515b62057eee6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to