Here is the problem.

There are 2 models

class RelationType(models.Model): title = models.CharField(max_length=40) class RelationRequest(models.Model): type_of_relation = models.ManyToManyField(RelationType, related_name='type_relation', verbose_name=_('type_relation')) 

That is, RelationRequest via MTM is associated with RelationType . type_of_relation comes to the model from the multi- type_of_relation . Here is the presentation:

views.py

 def post(self, request, *args, **kwargs): self.object = self.get_object() relation_form = RelationRequestForm(request.POST or None) if relation_form.is_valid(): for rt in relation_form.cleaned_data['type_of_relation']: relation_user_id = int(filter(lambda x: x.isdigit(), request.path)) rq = RelationRequest.objects.create( creator = request.user, relation = User.objects.get(id = relation_user_id), ) rq.type_of_relation.add(rt) 

When I look at the record in the admin panel, all the fields are filled in correctly. But when I try to get the type_of_relation field using

 request_relation = RelationRequest.objects.filter(creator=user.creator).filter(status=True).first().type_of_relation 

I get None . I look in the database table accounts_relationrequest - there is no field at all enter image description here

But there is a table accounts_relationrequest_type_of_relation enter image description here

In general, you need to somehow, knowing the id the RelationRequest entry, get the value of type_of_relation .

In general, in a good way, you need to remove this table and add a field with type_of_relation .

But how to do it, I do not know ...

    1 answer 1

    Value type_of_relation depending on the required format can be obtained

    either so:

     request_relation = RelationRequest.objects.filter(creator=user.creator).filter(status=True).values('type_of_relation') 

    QuerySet will contain pairs from <"type_of_relation": RelationType id> for RelationRequest records that match the filter conditions.

    either so:

     request_relation = RelationRequest.objects.filter(creator=user.creator).filter(status=True).values_list('type_of_relation', flat=True) 

    In this case, the QuerySet will have a flat list of RelationType id entries for RelationRequest entries that match the filter conditions.

    If you want to get type_of_relation for the first RelationRequest object as in your example, then it is better to do this:

     request_relation = RelationRequest.objects.filter(creator=user.creator).\ filter(status=True).prefetch_related('type_of_relation').first() 

    and after request_relation.type_of_relation.all() get a QuerySet with the necessary objects.

    The table accounts_relationrequest_type_of_relation is created automatically for the m2m link, you should not delete it.

    • Thanks, but this is not exactly that. I need for one particular entry. I didn’t point out first() for nothing. This is the last modified (saved entry), since the sorting is based on updates ... - m0nte-cr1st0 pm
    • Thank. Corrected your answer. - m0nte-cr1st0 1:22 pm
    • one
      Updated the answer with an example for one RelationRequest object. - avtomato 5:57 pm
    • Can you explain why when viewing values() or values_list() these values ​​are not visible, but they are there, it turns out ...? - m0nte-cr1st0 9:22 pm
    • Need to get a list of values ​​instead of id ? - avtomato