0

I'm new to Django and I'm in the process of converting a PHP project into Python etc.

I'm trying to do something super simple, but I keep getting the following error:

AttributeError at /news/1/
'QuerySet' object has no attribute 'slug'

Here is my most of my Model to help explain:

class Article(models.Model):    
    title            = models.CharField(max_length=200)
    STATUS_CHOICES   = ((1,'Published'), (2,'Hidden'), (3,'Draft'))
    status           = models.IntegerField(choices=STATUS_CHOICES,default=3)
    pub_date         = models.DateField('date published')
    tags             = TaggableManager()
    header_width     = models.IntegerField(default=1,blank=True,null=True)
    header_height    = models.IntegerField(default=1,blank=True,null=True)
    header           = models.ImageField(upload_to='news/',width_field='header_width',height_field='header_height',blank=True,null=True)
    header_filter    = models.BooleanField('Enable filter',default=1)
    excerpt          = HTMLField(blank=True)
    body             = HTMLField(blank=True)
    custom_link_text = models.CharField(max_length=20,default="Read More")
    created_at       = models.DateTimeField(auto_now_add=True)
    updated_at       = models.DateTimeField(auto_now=True)
    slug             = AutoSlugField(unique=True,max_length=200,populate_from='db_slug',default="",slugify=return_value)

    def __str__(self):
        return self.title

Im currently just testing to pull through the slug, so my view method currently looks like this:

def detail_redirect(request, pk):   
    a = Article.objects.all().filter(pk=pk)
    return HttpResponse(a.slug)
    # return redirect('news:detail',args=(a.slug,pk))

The plan is for this method to redirect to another URL in my application. It queries the DB via the primary key and fetches the Slug, which I then pass on to the redirect shortcut.

It seems to be something that should just work, but it's not. It's really frustrating. The object i query appears to return ok. Because of my __str__ method it returns the title. But any other attributes throw and error. Could it be todo with visibility such as being private or protected?

I'm hoping it's something simple I'm missing. Let me know if you require more code/detail to help explain.

Thanks for taking the time to look at my question.

1 Answer 1

4

filter always returns a queryset, which is a list-like object potentially consisting of many items. Querysets do not have model attributes, only their members do. You should use get instead:

a = Article.objects.get(pk=pk)

(Note you don't need all(), in either version of the code.)

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

1 Comment

Thank you. That did the trick. I knew it must of been something that simple. I'm still learning Django so thanks for explaining your answer. It will come in handy for next time :)

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.