Below is my models.py file in a Django project. Whenever I try to run the project I get the following error. Please assist as I have just started picking up django I tried changing the names of the foreign columns as suggested by the error but no avail. A lot of answers out there suggest changes with respect to related_name which does not exist in my case.
Error from console
app_fin.TblLockerCoutCin.jewel_item: (fields.E304) Reverse accessor for 'TblLockerCoutCin.jewel_item' clashes with reverse accessor for 'TblLockerCoutCin.jewel_item_code'. HINT: Add or change a related_name argument to the definition for 'TblLockerCoutCin.jewel_item' or 'TblLockerCoutCin.jewel_item_code'. app_fin.TblLockerCoutCin.jewel_item: (fields.E304) Reverse accessor for 'TblLockerCoutCin.jewel_item' clashes with reverse accessor for 'TblLockerCoutCin.jewel_item_name'. HINT: Add or change a related_name argument to the definition for 'TblLockerCoutCin.jewel_item' or 'TblLockerCoutCin.jewel_item_name'. app_fin.TblLockerCoutCin.jewel_item_code: (fields.E304) Reverse accessor for 'TblLockerCoutCin.jewel_item_code' clashes with reverse accessor for 'TblLockerCoutCin.jewel_item'. HINT: Add or change a related_name argument to the definition for 'TblLockerCoutCin.jewel_item_code' or 'TblLockerCoutCin.jewel_item'. app_fin.TblLockerCoutCin.jewel_item_code: (fields.E304) Reverse accessor for 'TblLockerCoutCin.jewel_item_code' clashes with reverse accessor for 'TblLockerCoutCin.jewel_item_name'. HINT: Add or change a related_name argument to the definition for 'TblLockerCoutCin.jewel_item_code' or 'TblLockerCoutCin.jewel_item_name'. app_fin.TblLockerCoutCin.jewel_item_name: (fields.E304) Reverse accessor for 'TblLockerCoutCin.jewel_item_name' clashes with reverse accessor for 'TblLockerCoutCin.jewel_item'. HINT: Add or change a related_name argument to the definition for 'TblLockerCoutCin.jewel_item_name' or 'TblLockerCoutCin.jewel_item'. app_fin.TblLockerCoutCin.jewel_item_name: (fields.E304) Reverse accessor for 'TblLockerCoutCin.jewel_item_name' clashes with reverse accessor for 'TblLockerCoutCin.jewel_item_code'. HINT: Add or change a related_name argument to the definition for 'TblLockerCoutCin.jewel_item_name' or 'TblLockerCoutCin.jewel_item_code'.
models.py
from django.db import models
class TblJewelleryInventory(models.Model):
id = models.IntegerField(primary_key=True)
item_code = models.CharField(unique=True, max_length=5, blank=True, null=True)
item_name = models.CharField(max_length=250, blank=True, null=True)
base_locker_location = models.CharField(max_length=45, blank=True, null=True)
updated_at = models.DateTimeField()
created_at = models.DateTimeField()
def __str__(self):
return self.name
class Meta:
managed = False
db_table = 'tbl_jewellery_inventory'
class TblJewelleryInventoryLogs(models.Model):
id = models.IntegerField(primary_key=True)
item_id = models.IntegerField()
item_code = models.CharField(unique=True, max_length=5, blank=True, null=True)
item_name = models.CharField(max_length=250, blank=True, null=True)
base_locker_location = models.CharField(max_length=45, blank=True, null=True)
updated_at = models.DateTimeField()
created_at = models.DateTimeField()
triggered_at = models.DateTimeField()
def __str__(self):
return self.name
class Meta:
managed = False
db_table = 'tbl_jewellery_inventory_logs'
class TblLockerCoutCin(models.Model):
jewel_item = models.ForeignKey(TblJewelleryInventory, models.DO_NOTHING)
jewel_item_code = models.ForeignKey(TblJewelleryInventory, models.DO_NOTHING, db_column='jewel_item_code')
jewel_item_name = models.ForeignKey(TblJewelleryInventory, models.DO_NOTHING, db_column='jewel_item_name')
reason = models.CharField(max_length=500, blank=True, null=True)
is_check_out = models.IntegerField(blank=True, null=True)
is_check_in = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
def __str__(self):
return self.name
class Meta:
managed = False
db_table = 'tbl_locker_cout_cin'
In your TblLockerCoutCin
model, both jewel_item_code
and jewel_item_name
fields have a many-to-one relationship with the TblJewelleryInventory
model (specifically to the pk
field). So, when an instance of TblJewelleryInventory
would try to traverse the reverse relationship for any one of the fields, it would try to use tbllockercoutcin_set
in both cases -- which would result in ambiguity as both reverse relations are referred by the same name.
In this case, you want to use a related_name
argument and set different accessor names for the fields e.g.:
jewel_item_code = models.ForeignKey(
TblJewelleryInventory,
on_delete=models.DO_NOTHING,
related_name='jewel_item_code', # Here
db_column='jewel_item_code',
)
jewel_item_name = models.ForeignKey(
TblJewelleryInventory,
on_delete=models.DO_NOTHING,
related_name='jewel_item_name', # Here
db_column='jewel_item_name',
)
You have to change the name of the related_name
...
Ex:
jewel_item_code = models.ForeignKey(
TblJewelleryInventory,
on_delete=models.DO_NOTHING,
related_name='re_jewel_item_code', # Here
db_column='jewel_item_code',
)
jewel_item_name = models.ForeignKey(
TblJewelleryInventory,
on_delete=models.DO_NOTHING,
related_name='re_jewel_item_name', # Here
db_column='jewel_item_name',
)
That's worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With