How to Solve Error Message Reverse accessor for ‘apps.Model.property’ clashes with reverse accessor for another ‘apps.Model.property’ when running Django Application

Posted on

Introduction

This is an article where there are errors appear. The errors appear while executing a Django internal server running a Django-based application. So, the following is the full error message appear while running a Django application using an internal Django server :

(env) C:\django\project\app>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\python-3.10\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\python-3.10\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\python-3.10\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\python-3.10\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\python-3.10\lib\site-packages\django\core\management\base.py", line 469, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
app.Room.host: (fields.E304) Reverse accessor for 'app.Room.host' clashes with reverse accessor for 'app.Room.topic'.
HINT: Add or change a related_name argument to the definition for 'base.Room.host' or 'base.Room.topic'.
app.Room.topic: (fields.E304) Reverse accessor for 'app.Room.topic' clashes with reverse accessor for 'app.Room.host'.
HINT: Add or change a related_name argument to the definition for 'base.Room.topic' or 'base.Room.host'.

System check identified 2 issues (0 silenced).

Before going on to the solution, the following is the actual content which is causing the error to appear. It is the ‘models.py’ file exist in the apps directory. That file has a model class with the name of Room as follows :

class Room(models.Model):
    host = models.ForeignKey(Topic, on_delete=models.SET_NULL, null=True)
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, null=True)

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)

    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    class Meta:
        ordering = ['-updated','-created']
    def __str__(self):
        return self.name

Solution

So, in order to solve the problem or the error messages, the solution is actually very simple. Just revise the ‘models.py’ file because of the property of the model class has a conflict. The conflict is becauase there are more than one which in this case two property with the same foreign key reference. Those property or attribute are ‘host’ and ‘topic’. Both of those property referring to the same class model for getting a foreign key from its id property. In other words, in order to solve the problem just change one of the property or attribute of the class model for foreign key reference. In this case, the ‘host’ actually referring to another class model which is the ‘User’ class model. So, just change the ‘models.py’ file as follows :

class Room(models.Model):
    host = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, null=True)

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)

    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    class Meta:
        ordering = ['-updated','-created']
    def __str__(self):
        return self.name

Leave a Reply