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

时间:2016-08-01 08:01:09

标签: python django

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

enter image description here

form.html

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

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

models.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)

views.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})

2 个答案:

答案 0 :(得分:3)

您应该在对象的模型中实现__str__(或{2},如果是Python 2)。

来自Django&#39; docs

  

Django在很多地方使用str(obj)。最值得注意的是,在Django管理站点中显示一个对象,并在显示一个对象时将值插入到模板中。因此,您应该始终从 str ()方法返回一个漂亮的,人类可读的模型表示。

答案 1 :(得分:0)

像这样更新你的models.py:

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)

  def __unicode__(self):
    return self.campaign_name  # or you can use any string value instead of campaign_name