N
N
night_terror2015-09-28 04:10:13
Django
night_terror, 2015-09-28 04:10:13

Q() object request not being executed?

There is the following model, which should describe the various characteristics of the product:

class CharacteristicType(models.Model):
    name = models.CharField(_(u'Name'), max_length=255)
    slug = models.SlugField(_(u'Slug'), max_length=150, unique=True,
                            help_text=_(u'Slug for product url created from name.'))

class Characteristic(models.Model):
    characteristic_type = models.ForeignKey(CharacteristicType)
    value = models.CharField(_(u'Value'), max_length=255)
    product = models.ForeignKey(Product, verbose_name=_(u'Product'),
                                help_text=_(u'Referenced product'))

I'm trying to run a query:
Product.objects.filter(Q(characteristic__characteristic_type__slug='brands') &  Q(characteristic__characteristic_type__slug = 'ingridient'))

which, in my understanding, should return a product that has two characteristics. But that doesn't happen. Though separately requests are fulfilled.
How to make such a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Klimenko, 2015-09-29
@night_terror

Oh, this is just a great request, for which there is a solution, but it is not the most obvious one:
https://docs.djangoproject.com/en/1.8/topics/db/qu... (highly recommend reading carefully)
I really had another situation, I calmly used filter sequences, and it led to incomprehensible selections.
in your case it should be like this:
and in the case of "feedback" or many-to-many relationships, these two separate filters are not the same as writing everything in one filter!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question