Question

I get this error: 'Searches' object has no attribute 'object'. I am using the generic ListView and I iterate over object_list in template. That is where the error comes in. My view is simple. Just attaching the model. All the relevant code is here.

Thanks

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'),
                       )

my model:

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)])

view:

class SearchListView(ListView):
    model=Searches

template:

{% extends "base.html" %}
{% block content %}

{% for obj in object_list %}
<p><a href="{{ obj.get_absolute_url }}">{{ obj }}</a></p>
{% endfor %}

{% endblock %}
Was it helpful?

Solution

The problem is on the line:

return reverse('postings.views.detail',args=[model_to_dict(self.object)])

Searches model doesn't really have an object attribute.

model_to_dict() needs a model instance:

model_to_dict(self)

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top