Reputation: 1376
I use the default User
class from from django.contrib.auth.models import User
. In the user creation when the user is created I set the username field to a random hash. When I use the model in ManyToMany field and use it in the Django admin then the random hash is rendered in the select box. Is there a way to specify the field to be taken from the model to be displayed in the select box so that I can specify the model as ManyToMany field and use the email to be rendered in the django admin form.
class TestModel(models.Model):
group = models.ManyToManyField(Group, null=True)
user = models.ManyToManyField(User, null=True)
Is there a field like the display_name that can be passed to the model field so that the appropriate field can be taken from the ManyToMany model for rendering. I am using Django 1.5.5
Upvotes: 2
Views: 806
Reputation: 2798
I think you need to do something like this,
class TestModelAdminForm(forms.ModelForm):
user = forms.MultipleChoiceField(choices=[ (user.id, user.email) for user in User.objects.all()])
class Meta:
model = TestModel
fields = ('user','group')
class TestModelAdmin(admin.ModelAdmin):
form = TestModelAdminForm
Upvotes: 4