G
G
Glitchmorphosis2019-10-21 23:17:29
Django
Glitchmorphosis, 2019-10-21 23:17:29

How to filter by list from ManyToMany?

There is a model with a field ManyToMany. From POSTthe request I get a list of pk records that are ManyToManyattached to. It pkis the object that is ManyToManyattached to. I need to receive only those records which were in this model.
So I'm looking for them:

json_request = json.loads(request.body)
exists_list = json_request['addId']
exists_list.append(json_request['mainId'])
print(exists_list)
try:
    exists_result = LinkedData.objects.filter(many__in=exists_list)
    print(exists_result) >>> 5, 823, 186, 585        
    return HttpResponse(exists_result)
except LinkedData.DoesNotExist:
    return HttpResponse('ok')

The entries ManyToManyare as pkfollows:
5, 823, 186, 585
In exists_list:
[83, 663, 853, 5]
exists_resultcontains the following entries:
5, 823, 186, 585
That is, it takes not only those objects that are in ( request exists_listbody ). POSTSo far, I've come up with to sort through QuerySetand look for matches with exists_list, and then what is not in exists_listdelete from QuerySet. Maybe there is a crutch way to do everything right?
Models:
class LinkedData(models.Model):
    many = models.ManyToManyField(
        ObjectDataFull,
        related_name='many_related',
        verbose_name='Много'
    )

    class Meta:
        db_table = 'build_analyzer_linked_data'

    def __str__(self):
        return u', '.join([str(a.pk) for a in self.many.all()])


class LinkedInfo(models.Model):
    linked_data = models.ForeignKey(
        LinkedData,
        on_delete=models.CASCADE,
        related_name='linked_data_related'
    )
    full = models.ForeignKey(
        ObjectDataFull,
        on_delete=models.CASCADE,
        related_name='unsorted_related'
    )

    main = models.BooleanField(
        default=False,
        null=False,
    )

    class Meta:
        db_table = 'build_analyzer_linked_info'

And is there any way to combine this into one line:
exists_list = json_request['addId']
exists_list.append(json_request['mainId'])

? When I do this:
exists_list = json_request['addId'].append(json_request['mainId'])

I get exists_list is None >>> True
JSON looks like this:
{
    "mainId": 5,
    "addId": [83, 663, 853]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2019-10-23
@Glitchmorphosis

This is the basic functionality of Django, and in the first snippet you use it quite correctly. It is not clear why the filtering does not occur, without casting the models and the relevant state of the database.
Possible reasons:

  1. ManyToMany.through-model is made by hand and does not have a unique composite index on 2 foreign keys
  2. All four objects actually have at least one of the filtered IDs
  3. You didn't provide all the code that describes the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question