from django.db import models

from django.contrib.contenttypes.models import ContentType


class MediaAssociationManager(models.Manager):
    
    def for_model(self, model, content_type=None):
        content_type = content_type or ContentType.objects.get_for_model(model)
        objects = self.get_query_set().filter(
            owner_content_type=content_type, 
            owner_object_id=model.pk,
        )
        return objects

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_unicode

class MediaAssociationManager(models.Manager):

    def for_model(self, model, content_type=None):
        """
        QuerySet for all media for a particular model (either an instance or
        a class).
        """
        ct = content_type or ContentType.objects.get_for_model(model)
        qs = self.get_query_set().filter(owner_content_type=ct)
        if isinstance(model, models.Model):
            qs = qs.filter(owner_object_id=force_unicode(model._get_pk_val()))
        return qs


Allow both instance and model classes in Media.objects.for_model()
Allow both instance and model classes in Media.objects.for_model()

  from django.db import models
+ from django.contrib.contenttypes.models import ContentType
+ from django.utils.encoding import force_unicode
  
- from django.contrib.contenttypes.models import ContentType
+ class MediaAssociationManager(models.Manager):
+ 
+     def for_model(self, model, content_type=None):
+         """
+         QuerySet for all media for a particular model (either an instance or
+         a class).
+         """
+         ct = content_type or ContentType.objects.get_for_model(model)
+         qs = self.get_query_set().filter(owner_content_type=ct)
+         if isinstance(model, models.Model):
+             qs = qs.filter(owner_object_id=force_unicode(model._get_pk_val()))
+         return qs
  
  
- class MediaAssociationManager(models.Manager):
-     
-     def for_model(self, model, content_type=None):
-         content_type = content_type or ContentType.objects.get_for_model(model)
-         objects = self.get_query_set().filter(
-             owner_content_type=content_type, 
-             owner_object_id=model.pk,
-         )
-         return objects
- 
Allow both instance and model classes in Media.objects.for_model()
## Code Before:
from django.db import models

from django.contrib.contenttypes.models import ContentType


class MediaAssociationManager(models.Manager):
    
    def for_model(self, model, content_type=None):
        content_type = content_type or ContentType.objects.get_for_model(model)
        objects = self.get_query_set().filter(
            owner_content_type=content_type, 
            owner_object_id=model.pk,
        )
        return objects

## Instruction:
Allow both instance and model classes in Media.objects.for_model()
## Code After:
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_unicode

class MediaAssociationManager(models.Manager):

    def for_model(self, model, content_type=None):
        """
        QuerySet for all media for a particular model (either an instance or
        a class).
        """
        ct = content_type or ContentType.objects.get_for_model(model)
        qs = self.get_query_set().filter(owner_content_type=ct)
        if isinstance(model, models.Model):
            qs = qs.filter(owner_object_id=force_unicode(model._get_pk_val()))
        return qs

