I am trying to use a raw sql query in a model manager method.
RawQuerySet lacks method _clone, is there a recommended way to return
a normal QuerySet for views to consume?

The models are included below. If there is a way to get the Django ORM
to make the M2M join via select_related and extra, I'd much rather use
that method, but I wasn't able to make it work.

Thanks,
Jeff

# models.py:

class Catalog(models.Model):
    title = models.CharField(max_length=50)
    slug = models.SlugField()
    users = models.ManyToManyField(User, blank=True,
                                   related_name='catalog_users')


class Facility(models.Model):
    catalog = models.ForeignKey(Catalog)
    title = models.CharField(max_length=50)
    slug = models.SlugField()
    address = models.CharField(max_length=255)


class Product(models.Model):
    sku = models.CharField(max_length=10)
    sku_internal = models.CharField(max_length=10)
    catalog = models.ForeignKey(Catalog)
    title = models.CharField(max_length=50)
    slug = models.SlugField()
    price_unit = models.DecimalField(max_digits=8,
                                     decimal_places=4)
    qty_increment = models.IntegerField()
    qty_allow_custom = models.BooleanField()
    description = models.CharField(max_length=255)
    image = models.ImageField(upload_to='images/',
                              blank=True)


class ProductManager(models.Manager):

    def products_for_username(self, username=None):
        sql = """SELECT myapp_product.id, myapp_product.sku,
                 myapp_product.sku_internal, myapp_product.catalog_id,
                 myapp_product.title, myapp_product.price_unit,
                 myapp_product.qty_increment,
                 myapp_product.qty_allow_custom,
                 myapp_product.description, myapp_product.image
                 FROM myapp_product
                 INNER JOIN myapp_catalog
                 ON myapp_product.catalog_id = myapp_catalog.id
                 INNER JOIN myapp_catalog_users
                 ON myapp_catalog.id = myapp_catalog_users.catalog_id
                 INNER JOIN auth_user
                 ON myapp_catalog_users.user_id = auth_user.id
                 WHERE auth_user.username = %s ORDER BY
myapp_product.sku"""
        return Product.objects.raw(sql, [username,])

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to