﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35272	Django Admin - changing input to <select> tag using ModelChoiceField posts an Object(1) in the form	DaveG	nobody	"I simplified the Location model for clarity but when I use the code below to modify the admin interface it works fine but on submitting the form the text of the value attribute in the <option> tag is not posted, an object is.  The Endpoint.sgln field just contains ""Object(1)"" and not the actual string from the <option> value attribute.  I added images of the admin form, the generated html and the resulting row in the database from a submission.  Not sure if this is a bug or a feature.

models.py

{{{
class Location(models.Model):
    sgln = models.CharField(max_length=15, unique=True)
    name = models.CharField(max_length=64)
    streetAddress = models.CharField(max_length=64)
    suite = models.CharField(max_length=64)
    city = models.CharField(max_length=32)
    state = models.CharField(max_length=2)
    postalCode = models.CharField(max_length=10)

class Endpoints(models.Model):
    sgln = models.CharField(max_length=19, unique=True)
    login = models.CharField('Endpoint Login ', max_length=32, default=None, null=True)
    password = models.CharField('Endpoint Password', max_length=32, default=None, null=True)
    tstamp = models.DateTimeField(auto_now_add=True)
}}}

forms.py

{{{
class SglnModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.sgln + "":"" + obj.name

class EndpointForm(forms.ModelForm): 
    sgln = SglnModelChoiceField(
        queryset=Location.objects.all(),
        to_field_name=""sgln"",
        label=""Endpoint SGLN"",
        required=True,
    )
    login = forms.CharField(label='Endpoint Login', max_length=32, required=True)
    password = forms.CharField(label='Endpoint Password', max_length=32, required=True)
}}}

admin.py

{{{
from django.contrib import admin
from .forms import EndpointForm
from .models import Endpoints

class EndpointAdmin(admin.ModelAdmin):
    form = EndpointForm

admin.site.register(Endpoints, EndpointAdmin)
}}}

the actual form as it appears in the admin interface:
[[Image(https://i.stack.imgur.com/vZfGW.png)]]

The HTML for the same form:
[[Image(https://i.stack.imgur.com/n4eqG.png)]]

The row inserted into the database:
[[Image(https://i.stack.imgur.com/ju8RY.png)]]
"	Bug	closed	Forms	5.0	Normal	invalid			Unreviewed	0	0	0	0	0	0
