Reputation: 338
Im trying to create relationship between class Locomotion and Section. Every Section has to have a king of Locomotion chosen (Class Locomotion is extended by Plain/Bus/Train etc.). And i'm getting the error about "Field defines a relation with model 'Locomotion', which is either not installed, or is abstract.". There is any way to do it? From what i read till now, there is no good way to do it, or i am mistaken?
My class Section:
class Section(RoadElements):
locomotion = models.ForeignKey(Locomotion, on_delete=models.CASCADE)
def __init__(self, *args, **kwargs):
super(Section, self).__init__(*args, **kwargs)
def __str__(self):
return self.name
which extends class RoadElements, abstract as well.
My definition of Locomotion:
class Locomotion(models.Model):
transportation_firm_name = models.CharField(max_length=200)
transportation_number = models.CharField(max_length=200)
departure_date_time = models.DateTimeField()
arrival_date_time = models.DateTimeField()
reservation = models.BooleanField(default=False)
class Meta:
abstract = True
And class extending, for example :
class Plain(Locomotion):
seat_number = models.CharField(max_length=200)
class_section = models.CharField(max_length=200)
Upvotes: 2
Views: 592
Reputation: 363566
You can not have a foreign key to an abstract base class, because there is no database table for that class. In your example: there will be a database table with rows for Plain
instances, but no table for Locomotion
.
The usual way to do this is using a Generic Foreign Key to point to one of the child classes.
Upvotes: 1