#28505: Django admin many to many recursive field widget shows identical objects
-------------------------------------+-------------------------------------
Reporter: Darshan | Owner: nobody
Singh Khalsa |
Type: Bug | Status: new
Component: Database | Version: 1.10
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have a scheduling app with a backend managed via Django Admin. This
production app runs okay on a dedicated server using Ubuntu 14.04 LTS,
Python2.7.3 with Django1.7.1 on a MySQL 5.5 database where the datatables
evolved with the app through scores of code updates and migrations.
The Members table for managing people and their role includes two
recursive fields that indicate whether a particular person has an employer
(ForeignKey) or is associated with one or more customers (ManyToMany).
I subsequently discovered that when creating a brand new MySQL database,
the Members table has flaws that render the Django Admin unuseable on that
table. The Members table in its original model had two recursive fields
that referred to fields within the "Member" table; current Django syntax
requires recursive fields to be in the "self" table.
The problem is that when adding or editing a Member via Django Admin, the
employer picklist and customers picklist fields display "Member object"
for each row instead of the desired list of various company names.
Same problem in Python2.7.3 with Django1.7.1 and in Python3.4.3 with
Django1.10.3.
I posted this problem 8 month ago on StackOverflow.com at
https://stackoverflow.com/questions/40954339/django-admin-many-to-many-
recursive-field-widget-shows-identical-objects but no one volunteered a
solution. The code snippets below create a minimum database that
illustrates the problem. I can provide more documentation and screenshots
if desired.
{{{
# models.py
class Member(models.Model):
first_name = models.CharField(max_length = 30)
last_name = models.CharField(max_length = 30)
role_choices = (
('Attendee', 'Attendee'),
('Customer', 'Customer'),
('Registrar', 'Registrar'),
)
now = timezone.now()
role = models.CharField(choices=role_choices, max_length=50)
created_date = models.DateTimeField(default=now)
employer = models.ForeignKey('self', null=True, blank=True,
limit_choices_to={'role': "Customer"})
customers = models.ManyToManyField('self', limit_choices_to=
{'role': "Customer"}, related_name="registrar_customers",
null=True, blank=True)
company_name = models.CharField(max_length=100, null=True, blank=True)
def __unicode__(self):
if self.role == "Customer":
name = self.company_name
elif self.role == "Attendee":
name = "{} - {}{}".format(self.employer.company_name,
self.first_name, self.last_name)
else:
name = self.first_name + " " + self.last_name
return name
# admin.py
class MemberAdminForm(forms.ModelForm):
class Meta:
model = Member
# Form override to force company names into picklists. This code
section does not work.
customers = forms.MultipleChoiceField(required=False)
def __init__(self, *args, **kwargs):
super(MemberAdminForm, self).__init__(*args, **kwargs)
cust_choices = ((obj.id, obj.company_name) for obj
in Member.objects.filter(role = 'Customer'))
self.fields['employer'].choices = cust_choices
self.fields['customers'].choices = cust_choices
fields = ['first_name', 'last_name', 'role',
'created_date', 'employer', 'customers',
'company_name']
class MemberAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'role',
'created_date', 'employer', 'company_name')
form = MemberAdminForm
admin.site.register(Member, MemberAdmin)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28505>
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/058.e5c644b4c7052a55e610891108fd7c57%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.