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
. Notice in the picture below it does not show the required values associated with the Conversation
model. 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).
The conversation
field is of type 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. Also it could be that because Django admin is not converting the input to string thus showing just Conversation object
in the drop down. Why is Django admin not showing the correct values for the chat
input field?
@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
Django admin shows the string representation of the object in the dropdown. This could be obtained by calling str(object)
. You can modify this behaviour by overriding the __str__
method in your class.
The implementation of the Django base model class ( django.db.models.Model
) has an implementation like below (for 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.
To change this behaviour you can override the __str__
method to get the desired value returned. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.