Si el modelo tiene una relación a si misma:
origin_watertank = models.ForeignKey ('self', null=True, on_delete=models.CASCADE, blank=True)
...no hay ningún problema, pero si hay dos relaciónes a sí misma en el mismo modelo:
origin_watertank = models.ForeignKey ('self', null=True, on_delete=models.CASCADE, blank=True)
comunication_point = models.ForeignKey ('self', null=True, on_delete=models.CASCADE, blank=True)
develve error.
Error:
ERRORS:
station.Station.comunication_point: (fields.E304) Reverse accessor for 'Station.comunication_point' clashes with reverse accessor for 'Station.origin_watertank'.
HINT: Add or change a related_name argument to the definition for 'Station.comunication_point' or 'Station.origin_watertank'.
station.Station.origin_watertank: (fields.E304) Reverse accessor for 'Station.origin_watertank' clashes with reverse accessor for 'Station.comunication_point'.
HINT: Add or change a related_name argument to the definition for 'Station.origin_watertank' or 'Station.comunication_point'.
Si el problema es que los dos tienen como nombre el mismo "self", pruebo cambiar uno de ellos por "self2" pero devuelve error:
Error:
ValueError: Cannot create form field for 'origin_watertank' yet, because its related model 'self2' has not been loaded yet
Archivo models.py
from django.db import models
from django import forms
from .managers import StationManager
# Create your models here.
class Station(models.Model):
timestamp_station = models.DateTimeField ('Fecha edición',null=True, blank=True)
code_station = models.CharField('Código',max_length=50,null=True, blank=True)
name_station = models.CharField('Nombre',max_length=50,null=True, blank=True)
origin_watertank = models.ForeignKey ('self', null=True, on_delete=models.CASCADE, blank=True)
comunication_point = models.ForeignKey ('self', null=True, on_delete=models.CASCADE, blank=True)
objects = StationManager()
class Meta:
verbose_name = 'Estación'
verbose_name_plural = 'Estaciones'
def __str__(self):
#return [self.name,self.area,self.codigo]
return str(self.id) + '-' + str(self.name_station)
Gracias.