2016-08-01 97 views
1

看來我不能得到下拉值而不是對象。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}) 

請指點。謝謝。

回答

3

您應該在對象的模型中實現__str__(或者如果Python 2爲__unicode__)。

從Django的docs

Django使用在許多地方STR(OBJ)。最值得注意的是,在Django管理站點中顯示對象,並在顯示對象時將其作爲插入到模板中的值。因此,你應該總是從str()方法中返回一個漂亮的,人類可讀的模型表示。

+0

我喜歡這個選項太多,但你也可以在模板中使用手冊的形式呈現。 – ubombi

+0

我先試一下。 –

+0

@ Dato'MohammadNurdin每次Django試圖顯示一個對象時,它都會查找它的Model __str__方法。該方法的返回值決定了對象的顯示方式。 – DeepSpace

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 
+0

我會先嚐試一下。 –

+0

我想要顯示這三樣東西,「FacebookAccount .account_description」,「FacebookFanPage.FacebookAccount .account_description」和「PredefinedMessage.list_name」。請檢查我的更新模型。 –

相關問題