1

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 %}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That is indeed where the problem is, but model_to_dict has no business being in that call in the first place.
@DanielRoseman agreed, it's just a start of the next set of questions/problems.
@DanielRoseman: I want to pass the the model attributes of this Searches model to another view, so that I could fetch the relevant postings from a different table. not sure if that is the right way to do. but designs questions are often closed or ignored in SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.