gpt4 book ai didi

python - Django - 如何显示下拉值而不是对象?

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:38 24 4
gpt4 key购买 nike

似乎我无法获得下拉值而不是对象。

enter image description here

表单.html

{% extends "base.html" %}
{% load i18n %}

{% block content %}
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock %}

模型.py

class FacebookAccount(models.Model):
user = models.ForeignKey(User)
account_description = models.CharField(max_length=50)
facebook_application_id = models.CharField(max_length=50)
facebook_application_secret = models.CharField(max_length=50)
ouath_token = models.CharField(max_length=50)

class FacebookFanPage(models.Model):
facebook_account = models.ForeignKey(FacebookAccount)
fan_page_description = models.CharField(max_length=50)
fan_page_id = models.CharField(max_length=30)

class PredefinedMessage(models.Model):
user = models.ForeignKey(User)
list_name = models.CharField(max_length=50)
list_description = models.CharField(max_length=50)

class Campaign(models.Model):
user = models.ForeignKey(User)
campaign_name = models.CharField(max_length=50)
autoresponder_type = (
('Send replies to inbox messages','Send replies to inbox messages'),
('Post replies to users comments','Post replies to users comments'),
)
facebook_account_to_use = models.ForeignKey(FacebookAccount)
set_auto_reply_for_fan_page = models.ForeignKey(FacebookFanPage)
message_list_to_use = models.ForeignKey(PredefinedMessage)
reply_only_for_this_keyword = models.CharField(max_length=50)

View .py

class AutoresponderForm(ModelForm):
class Meta:
model = Campaign
fields = ['campaign_name','facebook_account_to_use','set_auto_reply_for_fan_page','message_list_to_use','reply_only_for_this_keyword']
exclude = ('user',)

def autoresponder_create(request, template_name='form.html'):
form = AutoresponderForm(request.POST or None)
if form.is_valid():
form = form.save(commit=False)
form.user = request.user
form.save()
return redirect('autoresponder_list')
return render(request, template_name, {'form':form})

最佳答案

您应该在对象的模型中实现 __str__(如果是 Python 2,则为 __unicode__)。

来自 Django 的 docs :

Django uses str(obj) in a number of places. Most notably, to display an object in the Django admin site and as the value inserted into a template when it displays an object. Thus, you should always return a nice, human-readable representation of the model from the str() method.

关于python - Django - 如何显示下拉值而不是对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com