On Fri, 2006-10-27 at 17:10 +0000, [EMAIL PROTECTED] wrote:
> I've got basically the same problem (trying to have rankings available
> different places). I've been looking at Generic Relations and
> Content_type,  and think I get it on a conceptual level, but there's
> something I'm missing.
> 
> I'm passing the content_type (and an id) in the URL so the view will
> know what I'm dealing with:
> 
> (r'^vote/(?P<content_type>[-\w]+)/(?P<obj_id>\d+)/(?P<vote>up|down)/$',
> 'gretschpages.karma.views.vote'),
> 
> So then in the view I need to get whatever object is being voted on,
> right? I'm trying:
> obj = ContentType.objects.get_for_model(content_type)
> 
> but that throws 'str' object has no attribute '_meta'

That's correct behaviour for get_for_model(). It takes a model -- a
Python class -- not a content_type value (which is an integer). It maps
your Python models to the appropriate ContentType instance. You want to
go the other way: given a content_type id, get the ContentType model and
then map that back to the referenced model (see below).

> So how do I get that object?

You have the integer which is the primary key value for the ContentType
instance. So 

        ct = ContentType.objects.get(pk=content_type)
        
will give you a ContentType object back. Now you can use
ct.model_class() to get the model it is related to, or even

        ct.get_object_for_this_type(pk=obj_id)
        
to get the particular instance it relates to. This latter method is a
useful shortcut method which gets the appropriate model type (using
model_class()) and then applies the filter to that.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to