#13794: Django does not respect to_field's model on an inline model admin
-------------------------------------+-------------------------------------
     Reporter:  sebastien@…          |                    Owner:  gautier
         Type:  Bug                  |                   Status:  new
    Component:  Forms                |                  Version:
     Severity:  Normal               |  1.5-beta-1
     Keywords:  admin inline         |               Resolution:
  to_field formset                   |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Old description:

> The problem occurs in the function __unicode__ of ModelB
>
> When editing a ModelA instance in admin site, all ModelB instances have a
> modela_id set to the pk of the ModelA instance, instead of the value of
> the to_field's field. And, when accessing to the field modela of a ModelB
> instance, a DoesNotExist exception is raised.
>
> This problem does not occus in the shell :
> {{{
> In [3]: ModelA.objects.all()[0].modelb_set.all()[0].modela_id
> Out[3]: u'TRUC'
>
> In [6]: ModelB.objects.all()[0].modela_id
> Out[6]: u'TRUC'
> }}}
>

> See below to reproduce
>
> {{{
> models.py
>
> class ModelA(models.Model):
>     code = models.CharField(max_length=20, unique=True)
>
> class ModelB(models.Model):
>     modela = models.ForeignKey(ModelA, to_field="code")
>     position = models.IntegerField()
>
>     def __unicode__(self):
>         return u"%s" % self.modela
>
> admin.py
>
> class ModelBInlineAdmin(admin.TabularInline):
>     model = ModelB
>
> class ModelAAdmin(admin.ModelAdmin):
>     model = ModelA
>     inlines = [ModelBInlineAdmin]
>
> admin.site.register(ModelA, ModelAAdmin)
>
> }}}

New description:

 The problem occurs in the function `__unicode__` of `ModelB`

 When editing a `ModelA` instance in admin site, all `ModelB` instances
 have a `modela_id` set to the `pk` of the `ModelA` instance, instead of
 the value of the `to_field`'s field. And, when accessing to the field
 modela of a `ModelB` instance, a `DoesNotExist` exception is raised.

 This problem does not occur in the shell :
 {{{
 In [3]: ModelA.objects.all()[0].modelb_set.all()[0].modela_id
 Out[3]: u'TRUC'

 In [6]: ModelB.objects.all()[0].modela_id
 Out[6]: u'TRUC'
 }}}


 See below to reproduce

 {{{
 # models.py

 class ModelA(models.Model):
     code = models.CharField(max_length=20, unique=True)

 class ModelB(models.Model):
     modela = models.ForeignKey(ModelA, to_field="code")
     position = models.IntegerField()

     def __unicode__(self):
         return u"%s" % self.modela

 # admin.py

 class ModelBInlineAdmin(admin.TabularInline):
     model = ModelB

 class ModelAAdmin(admin.ModelAdmin):
     model = ModelA
     inlines = [ModelBInlineAdmin]

 admin.site.register(ModelA, ModelAAdmin)
 }}}

--

Comment (by aaugustin):

 Added a bit of markup for reaadability.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/13794#comment:9>
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 post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to