A newbie Django question about associating data in models.

一个关于在模型中关联数据的新手问题。

I have three tables, CarMake, CarModel and Driver.

我有三张桌子,CarMake, CarModel和Driver。

CarMake has several car brands in it.

CarMake有好几个汽车品牌。

CarModel has several models of each of those brands.

CarModel有几个不同品牌的车型。

  • My first question is how do I associate multiple car models per car brand inside a models.py class?

    我的第一个问题是,如何在一个车型中把每个车型的多个车型联系起来。py课吗?

  • Secondly, in the admin page (and eventually via the views) how do I associate those car makes and car models with a driver. So in Admin, If I make a new Driver, I choose driver_name, then select from a dropdown list car_make, then based on what Make I selected, choose from a dropdown list of car models.

    其次,在管理页面(最终通过视图)中,我如何将汽车制造和汽车模型与司机联系起来。所以在Admin中,如果我创建一个新的驱动程序,我选择driver_name,然后从下拉列表car_make中选择,然后根据make选择,从下拉列表中选择车型。

    For example in admin:

    例如在管理:

      driver name =    "john" 
    
      driver car = -> Toyota    -> Avalon
    
  • Third, how would I select multiple car makes and multiple car models per make for a driver. So if a driver selects Volkswagon model cars, they can select 'Golf' and 'Passat' from the dropdown list as their cars.

    第三,我如何为一个司机选择多款车和多款车。因此,如果司机选择大众车型,他们可以从下拉列表中选择“高尔夫”和“帕萨特”作为他们的车型。

Here is some basic code I was considering:

下面是我正在考虑的一些基本守则:

from django.db import models

class CarMake(models.Model):
    MAKE = (
        ('Ford'),
        (''Toyota'),
        ('Volkswagon'),
    )

    car_make = models.CharField(choice=MAKE)

class CarModel(models.Model):
    MODEL = (
        ('Ford'
            ('Focus'),
            ('Explorer'),
            ('F150'),
        )
        ('Toyota'
            ('Avalon'),
            ('Highlander'),
            ('Tundra'),
        )
        ('Volkswagon'
            ('Golf'),
            ('Jetta'),
            ('Passat'),
        )
    )

    car_model = models.CharField(choice=MODEL)

class Driver(models.Model):
    driver_name = models.CharField(max_length=120)
    driver_car = '''choose make and model'''

2 个解决方案

#1


1

Look into the Django Models Documentation to find out how to create your models.py. I see the exact idea of what you're getting at in this file but you're combining two things.

查看Django模型文档,了解如何创建模型。py。我知道你在这个文件中得到的确切概念但是你把两件事结合起来了。

models.py should contain the entities and how they are connected not the entities and the possible instances of these entities. models.py defines the database structure not its contents - this is important to note.

模型。py应该包含实体及其连接方式,而不是实体和这些实体的可能实例。模型。py定义的是数据库结构,而不是它的内容——注意这一点很重要。

As a few pointers:

一些指针:

  • CarModel should have a ForeignKey of the CarMake table. This will define the relationship between a car and who makes it. Then you will need to define another field to hold the model of that car. I'm not sure how to enforce only selecting cars from a certain manufacturer (but that's a question in it's own right) - maybe this should be in the front-end logic?
  • 汽车模型应该有一个外国钥匙的汽车制造表。这将定义一辆车与谁制造它之间的关系。然后你需要定义另一个字段来保存那辆车的模型。我不知道如何强制只从某个制造商选择汽车(但这是它自己的问题)——也许这应该是前端逻辑?
  • Driver should also have a ForeignKey of CarModel to define what car this driver drives. note how many cars can one driver drive? You'll need to decide if they can only drive one or if they can drive many.
  • 司机也应该有一个外国人的汽车模型,以定义什么汽车司机驾驶。注意有多少辆车可以驾驶?你需要决定他们是否只能驾驶一辆或者是否能开很多车。

Have a good read at the documentation before going on, it'll help you a great deal more than anything else and is always the best place to start.

在继续之前好好阅读一下文档,它将比其他任何东西都更有帮助,并且始终是最好的起点。

更多相关文章

  1. python之全栈开发——————IO模型
  2. 数据挖掘(三)分类模型的描述与性能评估,以决策树为例
  3. 机器学习教程之2-k近邻模型的sklearn实现
  4. Django模型选择:使用元组的第一个元素
  5. 【懒懒的Tensorflow学习笔记三之搭建简单的神经网络模型】
  6. Django代理模型返回父模型
  7. Tensorflow:恢复图形和模型,然后在单个图像上运行评估
  8. [caffe(二)]Python加载训练caffe模型并进行测试2
  9. 新人求助:tensorflow官方文档里构建第一个多层卷积网络,训练和评估

随机推荐

  1. Android——SharedPreferences
  2. android 常用布局有哪些
  3. Android工程导入时常见的错误解决方法
  4. Android ellipsize属性(多余文字用省略号
  5. android限制文本长度
  6. android ListView 示例1 entries 指定一
  7. Android:解决ListView按下后上下滑动背景
  8. Android studio was unable to find a va
  9. Android 使用iperf测试wifi吞吐量
  10. 二、 Android中gravity与layout_gravity