Django — модифицированная форма администратора, возвращающая объект вместо текстаPython

Программы на Python
Anonymous
Django — модифицированная форма администратора, возвращающая объект вместо текста

Сообщение Anonymous »


I am trying to customize an Admin form on a Django site so that one of the fields is changed from a text field to a select widget pre-populated by grabbing all the serial numbers from a table of locations (each has a unique number).

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 form looks fine: Django Admin Form

The HTML looks fine: [HTML picture] (Изображение)

But the database is showing a "object(1)" as being inserted along with the rest of the fields. This should be a serial number pulled form the select tag. Database row

Any hints as to why this is happening?


Источник: https://stackoverflow.com/questions/781 ... ad-of-text

Вернуться в «Python»