[英]Django admin not showing choices for Foreign key field
I am creating a small chat app and I have problems making a message in the admin because of the drop down for the Messagemie
model for field chat
. 我正在创建一个小型聊天应用程序,由于用于现场
chat
的Messagemie
模型的下拉列表,我在管理员中创建消息时遇到问题。 Notice in the picture below it does not show the required values associated with the Conversation
model. 请注意,在下面的图片中,它没有显示与“
Conversation
模型关联的必需值。 The values that the conversation
field in the Conversation
model accepts are of the form "number-number", eg 5-10
, 11-21
etc. Note that I have created a mechanism not shown below which converts such input formats to strings for non Django admin input (When users start a new conversation). 该值
conversation
在现场Conversation
模型接受的形式为“数编号”,例如5-10
, 11-21
等。注意,我已经创建了一个机构未示出,低于该这样的输入格式转换成用于非字符串Django管理员输入(当用户开始新对话时)。
The conversation
field is of type CharField
. conversation
字段的类型为CharField
。 I suspect the reason why Django admin form does not show the required values is because of the field
type, however I am not sure. 我怀疑Django管理表单未显示所需值的原因是由于
field
类型,但是我不确定。 Also it could be that because Django admin is not converting the input to string thus showing just Conversation object
in the drop down. 也可能是因为Django管理员没有将输入转换为字符串,因此在下拉列表中仅显示了
Conversation object
。 Why is Django admin not showing the correct values for the chat
input field? 为什么Django管理员未为
chat
输入字段显示正确的值?
@python_2_unicode_compatible
class Conversation(models.Model):
conversation = models.CharField(unique=True, max_length=150)
email_1 = models.ForeignKey(Usermie, to_field="email", related_name="email_1_convo")
email_2 = models.ForeignKey(Usermie, to_field="email", related_name="email_2_convo")
@python_2_unicode_compatible
class Messagemie(models.Model):
sender = models.ForeignKey(Usermie, to_field="email", related_name="email_sender")
receiver = models.ForeignKey(Usermie, to_field="email", related_name="email_receiver")
# The username is the sender's username
sender_username = models.CharField( max_length=50)
receiver_username = models.CharField(max_length=50)
message = models.TextField()
chat = models.ForeignKey(Conversation, to_field="conversation", related_name="conversation_chat")
Picture showing Messagemie model chat
field selection in admin 该图显示了管理员中Messagemie模型
chat
字段的选择
Picture of input values in Conversation
model Django admin. Conversation
模型Django admin中输入值的图片。
Django admin shows the string representation of the object in the dropdown. Django admin在下拉列表中显示对象的字符串表示形式。 This could be obtained by calling
str(object)
. 这可以通过调用
str(object)
。 You can modify this behaviour by overriding the __str__
method in your class. 您可以通过重写类中的
__str__
方法来修改此行为。
The implementation of the Django base model class ( django.db.models.Model
) has an implementation like below (for python3) - Django基本模型类(
django.db.models.Model
)的实现具有以下类似的实现(对于python3)-
def __str__(self):
return str('%s object' % self.__class__.__name__)
which explains what you see. 解释您看到的内容。
self.__class__.__name__
evaluates to "Conversation"
, hence you end up seeing "Conversation object"
in the dropdown. self.__class__.__name__
计算结果为"Conversation"
,因此您最终在下拉列表中看到"Conversation object"
。
To change this behaviour you can override the __str__
method to get the desired value returned. 若要更改此行为,可以重写
__str__
方法以获取所需的值。 One sample implementation is below. 下面是一个示例实现。 You could modify the method easily to do include any logic you want.
您可以轻松地修改方法以包含所需的任何逻辑。
class Conversation(models.Model):
conversation = models.CharField(unique=True, max_length=150)
email_1 = models.ForeignKey(Usermie, to_field="email",
related_name="email_1_convo")
email_2 = models.ForeignKey(Usermie, to_field="email",
related_name="email_2_convo")
def __str__(self):
return self.conversation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.