#9144: Bug with formsets validation in admin with models with m2m through other
model
----------------------------------+-----------------------------------------
Reporter: msaelices | Owner: nobody
Status: new | Milestone:
Component: django.contrib.admin | Version: SVN
Keywords: | Stage: Unreviewed
Has_patch: 0 |
----------------------------------+-----------------------------------------
Is very difficult to reproduce. In my case I only can reproduce in
production server (apache). It depends on default db ordering if you don't
put a {{{ordering = ('id',)}}} in Model.
These are my models:
{{{
# models.py
class Team(models.Model):
name = models.CharField(_('name'), max_length=200,
help_text=_('Enter name'))
members = models.ManyToManyField(User, verbose_name=_('members'),
related_name=_('teams'),
through='Membership')
class Membership(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)
team = models.ForeignKey(Team)
date_joined = models.DateField(default=date.today)
# admin.py
class MembershipInline(admin.TabularInline):
model = Membership
class TeamAdmin(admin.ModelAdmin):
inlines = [
MembershipInline,
]
admin.site.register(Team, TeamAdmin)
}}}
Problem appears when you try to change admin form with to inlines
Membership in a Team.
I attached a capture of error.
The validation error is this (i have to use pdb in
{{{contrib.admin.options.change_view}}}, because validation error doesnt
appears on web page):
{{{
formsets[0].errors
[{'id': [u'Membership with this None already exists.']}, {}, {}, {}, {}]
}}}
I think ''None'' is another simple labeling issue, but is not the case.
The error appearing depends on DB configuration, because the ordering in
formsets depends on exact DB engine and version. Further investigation
show me that the problem was in uniqueness checkin for inline itself. In
one configuration, the ID in form match with ID in database, but in
production server not.
I get fixed the issue on my project changing Membership model like this:
{{{
class Membership(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)
team = models.ForeignKey(Team)
date_joined = models.DateField(default=date.today)
class Meta:
ordering = ('id',)
}}}
Now, the ordering is fixed to ID and works well in both configurations. I
think a quick fix was define a default ordering if you don't define any
order in inlines creation. It's a little hacky, of course.
--
Ticket URL: <http://code.djangoproject.com/ticket/9144>
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---