我收到此错误:
'Searches' object has no attribute 'object'
。我正在使用generic ListView
,我在object_list
中迭代template
。这就是错误的来源。我的view
很简单。只需附加model
即可。所有相关的代码都在这里。
由于
urlpatterns的:
urlpatterns = patterns('',
url(r'^create/$','customsearches.views.create_search' , name='create_search'),
url(r'^list/$', SearchListView.as_view(template_name='search_list.html'), name='search_list'),
)
我的模特:
class Searches(models.Model):
SELLER_CHOICES=(('OWNER','owner'),
('DEALER','dealer'),
('BOTH','both'), )
#search_id = models.IntegerField(primary_key=True)
user = models.ForeignKey(User)
make = models.CharField(max_length=100, blank=True)
model = models.CharField(max_length=100, blank=True)
keywords = models.CharField(max_length=100, blank=True)
max_price = models.IntegerField(blank=True, null=True)
min_price = models.IntegerField(blank=True, null=True)
max_year = models.IntegerField(blank=True, null=True)
min_year = models.IntegerField(blank=True, null=True)
pic_only = models.NullBooleanField()
search_title_only = models.NullBooleanField()
owner_dealer_all = models.CharField(max_length=10,choices=SELLER_CHOICES,verbose_name='owner/dealer')
class Meta:
#managed = False
db_table = 'Searches'
verbose_name_plural = "Searches"
def __unicode__(self):
return "%s %s %s-%s" %(self.make,self.model,self.max_year,self.min_year)
def get_absolute_url(self):
return reverse('postings.views.detail',args=[model_to_dict(self.object)])
视图:
class SearchListView(ListView):
model=Searches
模板:
{% extends "base.html" %}
{% block content %}
{% for obj in object_list %}
<p><a href="{{ obj.get_absolute_url }}">{{ obj }}</a></p>
{% endfor %}
{% endblock %}
答案 0 :(得分:1)
问题在于:
return reverse('postings.views.detail',args=[model_to_dict(self.object)])
Searches
模型确实没有object
属性。
model_to_dict()需要一个模型实例:
model_to_dict(self)
希望有所帮助。