如何在Django项目中建立模型之间的正确关系

huangapple go评论58阅读模式
英文:

How to create right relations between models in Django project

问题

以下是翻译好的部分:

我正在开发Django项目其中包括以下模型

class Aircraft_fleet(models.Model):
    Aircraft_type = models.CharField(max_length=16)
    Aircraft_Serial_Number = models.CharField(max_length=16)
    Engine_1_Ser_Num = models.ForeignKey('Engine', related_name='Engine_1', on_delete=models.PROTECT)
    Engine_2_Ser_Num = models.ForeignKey('Engine', related_name='Engine_2', on_delete=models.PROTECT)

class Engine(models.Model):
    Ser_Num = models.PositiveIntegerField()
    Flight_Num = models.CharField(max_length=80)
    Flight_Date = models.DateField()
    Oil_Quantity_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Quantity_Arrival = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Arrival = models.DecimalField(max_digits=5, decimal_places=2)

一架飞机Aircraft_fleet模型有两个不同的引擎Ser_Num特定引擎Ser_NumEngine模型仅适用于一架特定的飞机
问题在于数据库中,“Engine表对于每个引擎Ser_Num都有多条记录
请参考下面的图片
[数据库中的Engine模型如下](https://i.stack.imgur.com/CwADJ.jpg)

我无法确定如何创建这两个模型之间的正确关系

当前关系是一对多的但它不起作用出现以下错误

django.db.utils.IntegrityError: 表'oilcons_aircraft_fleet'中主键为'1'的行具有无效的外键oilcons_aircraft_fleet.Engine_2_Ser_Num_id包含一个值'193262'而该值在oilcons_engine.id中没有相应的值
英文:

I am developing Django progect with following models:

class Aircraft_fleet(models.Model):
    Aircraft_type = models.CharField(max_length=16)
    Aircraft_Serial_Number = models.CharField(max_length=16)
    Engine_1_Ser_Num = models.ForeignKey('Engine', related_name='Engine_1', on_delete=models.PROTECT)
    Engine_2_Ser_Num = models.ForeignKey('Engine', related_name='Engine_2', on_delete=models.PROTECT)
class Engine(models.Model):
    Ser_Num = models.PositiveIntegerField()
    Flight_Num = models.CharField(max_length=80)
    Flight_Date = models.DateField()
    Oil_Quantity_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Quantity_Arrival = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Arrival = models.DecimalField(max_digits=5, decimal_places=2)

One Aircraft (Aircraft_fleet model) have two Engine Ser_Num (different of course). Particular Engine Ser_Num (Engine model) is fitted only on one partucular Aircraft.
The thing is that in database, "Engine" table has multiple records for each Engine "Ser_Num".
See picture below:
Engine Model in database look like:

I can't determine how to create the proper relations between these two models.

Currently, relation is one to many, but it doesn't work. Following error apears:

django.db.utils.IntegrityError: The row in table 'oilcons_aircraft_fleet' with primary key '1' has an invalid foreign key: oilcons_aircraft_fleet.Engine
_2_Ser_Num_id contains a value '193262' that does not have a corresponding value in oilcons_engine.id.

答案1

得分: 0

理解你的意思了,engine_ser_num 不属于 Aircraft_fleet,而属于 Engine

请使用以下代码:

class Aircraft_fleet(models.Model):
    Aircraft_type = models.CharField(max_length=16)
    Aircraft_Serial_Number = models.CharField(max_length=16)

class Engine(models.Model):
    aircraft = models.ForeignKey('Aircraft_fleet', on_delete=models.CASCADE)
    Ser_Num = models.PositiveIntegerField()
    Flight_Num = models.CharField(max_length=80)
    Flight_Date = models.DateField()
    Oil_Quantity_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Quantity_Arrival = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Arrival = models.DecimalField(max_digits=5, decimal_places=2)

现在一个 Engine 可以与一个 Aircraft_fleet 相关联:

e = Engine.objects.all().first()
print(e.aircraft.Aircraft_type)

一个 Aircraft_fleet 可以与多个 Engine 相关联:

af = Aircraft_fleet.objects.all().first()
for engine in af.engine_set.all():
    print(engine.Ser_num)

请告诉我进展如何。

重要修改:

确保你正确执行了数据库迁移,并进行了迁移操作。因为IntegrityError可能也是由数据库中仍然存在“旧”关系的“旧”数据引起的。

英文:

Okay if I understand you correctly the engine_ser_num does not belong to the Aircraft_fleet but to the Engine.

Go with this:

class Aircraft_fleet(models.Model):
    Aircraft_type = models.CharField(max_length=16)
    Aircraft_Serial_Number = models.CharField(max_length=16)

class Engine(models.Model):
    aircraft = models.ForeignKey('Aircraft_fleet', on_delete=models.CASCADE)
    Ser_Num = models.PositiveIntegerField()
    Flight_Num = models.CharField(max_length=80)
    Flight_Date = models.DateField()
    Oil_Quantity_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Quantity_Arrival = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Arrival = models.DecimalField(max_digits=5, decimal_places=2)

Now one Engine can be related to one Aircraft_fleet

e = Engine.objects.all().first()
print(e.aircraft.Aircraft_type)

And one Aircraft_fleet can be related to multiple Engines

af = Aircraft_fleet.objects.all().first()
for engine in af.engine_set.all():
    print(engine.Ser_num)

Let me know how it goes.

important edit:

For sure you need to do the migrations correctly and migrate it. Because that IntegrityError could also be caused by "old" data inside the database that still have "old" relationships.

答案2

得分: 0

以下是翻译后的代码部分:

class Aircraft(models.Model):
    Aircraft_type = models.CharField(max_length=16)
    Aircraft_Serial_Number = models.CharField(max_length=16)

航空器可能有2/3/4个引擎:
```python
class Engine(models.Model):
    Ser_Num = models.PositiveIntegerField()
    aircraft = models.ForeignKey('Aircraft', ....)   # 引擎属于一架飞机
    ...
class Flight(models.Model):
    Flight_Num = models.CharField(max_length=80)
    Flight_Date = models.DateField()
class EngineDataRecord(models.Model):
    flight = models.ForeignKey('Flight', ....)     # 引擎数据记录适用于特定航班
    engine = models.ForeignKey('Engine', ....)     # 和特定引擎有关
    Oil_Quantity_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Quantity_Arrival = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Arrival = models.DecimalField(max_digits=5, decimal_places=2)
英文:

possible structure:

class Aircraft(models.Model):
    Aircraft_type = models.CharField(max_length=16)
    Aircraft_Serial_Number = models.CharField(max_length=16)

aircraft may have 2/3/4 ... engines:

class Engine(models.Model):
    Ser_Num = models.PositiveIntegerField()
    aircraft = models.ForeignKey('Aircraft', ....)   # engine belongs to an aircraft
    ...
class Flight(models.Model):
    Flight_Num = models.CharField(max_length=80)
    Flight_Date = models.DateField()
class EngineDataRecord(models.Model):
    flight = models.ForeignKey('Flight', ....)     # Engine Data record is for a certain flight
    engine = models.ForeignKey('Engine', ....)     # and a certain engine
    Oil_Quantity_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Quantity_Arrival = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Departure = models.DecimalField(max_digits=5, decimal_places=2)
    Oil_Temperature_Arrival = models.DecimalField(max_digits=5, decimal_places=2)

huangapple
  • 本文由 发表于 2023年2月10日 16:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定