[英]Django ModelChoiceField displaying values in django.contrib.auth and a Profile table
I am extremely new to python or django. 我对python或django非常陌生。 I inherited some code that needs some "fixing" and I could use some help.
我继承了一些需要“修复”的代码,我可以使用一些帮助。
Here's the situation: 情况如下:
We have a drop-down box from which you can choose multiple users. 我们有一个下拉框,您可以从中选择多个用户。 Currently, the users are displayed as usernames.
当前,用户显示为用户名。 We would like for them to be displayed as Full Names.
我们希望它们以全名显示。
The Issue: The username is coming from: django.contrib.auth
问题:用户名来自:
django.contrib.auth
The First Name and Last Name are coming from the Profile Model. 名字和姓氏来自配置文件模型。 Here's what the Profile Model looks like:
概要文件模型如下所示:
class Profile(models.Model):
CURRENTLY_STUDENT = "STU"
CURRENTLY_PROFESSOR = "PFR"
CURRENTLY_CHOICES = [
(CURRENTLY_STUDENT, "Student"),
(CURRENTLY_PROFESSOR, "Professor"),
]
user = models.OneToOneField(User, related_name="profile")
currently_am = models.CharField(max_length=3, choices=CURRENTLY_CHOICES, default=CURRENTLY_STUDENT)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
If you have to display a full name we use the following: 如果必须显示全名,请使用以下命令:
user.profile.display_name
where display_name is: 其中display_name是:
def display_name(self):
name = "%s %s" % (self.first_name, self.last_name)
return name
Following is the code for the drop-down box in forms.py: 以下是forms.py中的下拉框代码:
from django import forms
from django.contrib.auth.models import User
from account.utils import user_display
class UserMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return user_display(obj)
class ShareForm(forms.Form):
participants = UserMultipleChoiceField(
queryset=User.objects.none(),
label="",
widget=forms.SelectMultiple(
attrs={
"data-placeholder": "Choose members.. "
}
)
)
Following is the code from account/utils.py for user_display
以下是来自account / utils.py的
user_display
代码
from account.conf import settings
def user_display(user):
return settings.ACCOUNT_USER_DISPLAY(user)
and here's the code from account/conf.py 这是来自account / conf.py的代码
class AccountAppConf(AppConf):
USER_DISPLAY = lambda user: user.username
You can't use obj.get_full_name()
as the First Name and Last Name are not coming from django.contrib.auth instead they are stored in the above mentioned Profile Model. 您不能使用
obj.get_full_name()
因为“名字”和“姓氏”不是来自django.contrib.auth,而是存储在上述配置文件模型中。
You can't use return obj.profile.display_name() either as the ModelChoiceField is drawn on django.contrib.auth. 您不能使用return obj.profile.display_name(),因为ModelChoiceField是在django.contrib.auth上绘制的。
Could somebody please help me with this? 有人可以帮我吗? How can I display full-names in the drop-down boxes and still pass the "user" as the value once someone makes a choice from the dropdown and hits submit?
当有人从下拉菜单中选择并点击提交后,如何在下拉框中显示全名并仍将“用户”作为值传递?
Thank you for your help :) 谢谢您的帮助 :)
Well you can assign custom choices as: 好吧,您可以将自定义选项分配为:
choices = [(obj.id, obj.profile.display_name()) for obj in User.objects.all()]
participants = UserMultipleChoiceField(
queryset=User.objects.none(),
label="",
widget=forms.SelectMultiple(
attrs={
"data-placeholder": "Choose members.. "
}
),
choices=choices
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.