0

Problem: Joining 2 models in Django.

Error: Error during template rendering. Direct assignment to the reverse side of a many-to-many set is prohibited. Use entity_id.set() instead.

I have read through all the threads on SO. Tried all the suggested solutions, read the Django documentation and think I just must be fundamentally misunderstanding something. Any help would be much appreciated.

I have 2 models. Entity and File.

An Entity can have multiples Files but each File only has 1 Entity.

The primary keys of each table are just auto incrementing integers. Therefore I want to join column entity_id from File with entity_id from Entity. According to the documentation I have set entity_id in File as a ForeignKey. And I have set entity_id as unique in Entity

class Entity(models.Model):
    pk_entity = models.AutoField(primary_key=True)
    entity_id = models.IntegerField(blank=True, null=True, unique=True)  
    name = models.CharField(blank=True, null=True)
    
    class Meta:
        managed = False
        db_table = 'entities'


class File(models.Model):
    pk_file = models.AutoField(primary_key=True)
    filename = models.CharField(blank=True, null=True)    
    entity_id = models.ForeignKey(Entity, on_delete= models.CASCADE, to_field='entity_id')
    

The view is just trying to render this. I have tried using .all() rather than select_related() but no data renders.

class TestListView(ListView):  
    queryset = File.objects.select_related()    
    template_name = "operations/files/test_list.html"

And this is the html:

{% extends "base.html" %}
{% block content %}
<div>
    <div>       
        <ul>
            {% for x in object_list %}
            <li>             
                {{x}} 
             </li>
            {% empty %}
            <p>Empty</p>
            {% endfor %}
        </ul>      
    </div>
</div>
{% endblock %}
3
  • 1
    have you tried checking the content of object_list, query_set and make sure the database is filled? Because .all() doesn't care about foreign keys, and if there's an error related to the foreign key it should put you an error message. Commented Jan 31, 2023 at 9:55
  • The database is populated. All() returns the File data but not the Entity data. Presently I am using selected_related() as the documentation and other threads on SO suggest this is the correct one to pull in FK Commented Jan 31, 2023 at 10:02
  • I would also check whether object_list is populated and checking the content of File.objects.select_related() on some test.py file to decide whether it's queryset issue or html rendering issue Commented Jan 31, 2023 at 10:12

1 Answer 1

2

When using select_related you should pass your field name to be selected.

Try this:

class TestListView(ListView):  
    queryset = File.objects.select_related("entity").all()    
    template_name = "operations/files/test_list.html"
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. When I try this it says "entity" not found and that I should use "entity_id" but then when I use "entity_id" a template error is thrown where Django is looking for "entity_id_id" i.e. somewhere an extra _id is being added
There in no need to declare you FK with _id postfix. In your model, you can declare entity instread of entity_id and then you sill access entity id via entity_id @Andy
In the FK declaration when I change from entity_id= to entity = and call as you describe then I get the same error as in the original post
Add your error traceback @Andy
I stumbled through this and got it working. There was a warning about name clash. I think the automatic appending of _id was confusing things. Your answer was critical to this. So thank you and I will choose this as accepted answer as I am sure it will help others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.