一个字典的下拉Django的价值观 [英] Values of a dict in a dropdown django
本文介绍了一个字典的下拉Django的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的models.py如下:
My models.py is as follows:
class SellerMarketplaces(models.Model):
id = models.IntegerField(db_column='ID',primary_key=True) # Field name made lowercase.
seller_id = models.ForeignKey('Sellerdetails',db_column='Seller_ID') # Field name made lowercase.
mk_id = models.ForeignKey('Marketplace',db_column='Mk_ID') # Field name made lowercase.
username = models.CharField(db_column='Username', max_length=100, blank=True, null=True)
我有以下的code。在我的意见
I have the following code in my views
def marketplaces(request):
seller = request.GET.get('seller', '')
allmarketplaces = SellerMarketplaces.objects.filter(seller_id = seller).values('mk_id__marketplace')
print allmarketplaces
,通过它,我发现了以下的输出:
through which I'm getting the following output:
[{'mk_id__marketplace': u'Flipkart'}, {'mk_id__marketplace': u'Snapdeal'}]
我想显示的值,即,Flipkart和Snapdeal在下拉,但我无法访问的值,当我写
I want to display the values i.e., Flipkart and Snapdeal in a dropdown but I'm unable to access the values, when I'm writing
print allmarketplaces.values()
它给了我
[{'username': u'mail@fields.com', u'seller_id_id': 1L, u'mk_id_id': 1L, 'id': 1L}, {'username': u'mail@fields.com', u'seller_id_id': 1L, u'mk_id_id': 2L, 'id': 2L}]
我怎么能访问值(Flipkart&安培; Snapdeal)?
how can i access the values (Flipkart & Snapdeal)?
推荐答案
您正在寻找的 values_list
:
You're looking for values_list
:
>>> allmarketplaces = SellerMarketplaces.objects.filter(seller_id=seller).values_list('mk_id__marketplace', flat=True)
>>> print allmarketplaces
['Flipkart', 'Snapdeal']
这篇关于一个字典的下拉Django的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文