简体   繁体   English

在创建两个模型后,在两个模型之间创建关系。 (Python,Django,SQLite)

[英]Creating a relationship between two models after both models have been created. (Python, Django, SQLite)

Explaining based on the django tutorial for creating a library: 根据用于创建库的django教程进行解释:

Say you allow a user to add a Book model to your library database. 假设您允许用户将Book模型添加到库数据库。 However you don't want them to add a Genre model to the Book . 但是,您不希望他们在Book添加Genre模型。 Instead, you have a Genre model in your database already with all listed Genre 's in the world. 相反,您的数据库中已有一个Genre模型,已经包含了世界上所有列出的Genre When the user adds a Book to your database, you want your database to add the proper Genre to your Book based on text in the Book 's summary. 当用户将Book添加到数据库时,您希望数据库根据Book的摘要中的文本将适当的Genre添加到Book中。 Say the Book has a TextField named summary and it has the words science and fiction in it when a bookinstance is created. 假设Book有一个名为summary的TextField ,并且在创建bookinstance时它包含了sciencefiction The database will then add a Genre of science fiction to your bookinstance based on the words found in the bookinstance's summary TextField . 然后,数据库将根据bookinstance的摘要TextField的单词向您的bookinstance添加一个science fiction Genre Whether this happens at the moment of bookinstance creation or immediately after doesn't matter to me. 这种情况发生在创建实例或紧接着发生时对我来说无关紧要。

I am trying to do this same thing with a website that would handle logging/creating workouts. 我正在尝试用一个可以处理日志记录/创建训练的网站做同样的事情。 When a user adds a workout (ie book to the library) to the database, I would like the database to add a movement (ie genre for books) to the workout depending upon what text is in the workout TextField . 当用户将workout (即书籍到图书馆)添加到数据库时,我希望数据库根据锻炼TextField中的文本向锻炼添加movement (即书籍的类型)。 Example below: 示例如下:

I add a Workout to the website with the following information as a TextField : 我将一个Workout添加到网站,其中包含以下信息作为TextField

5 rounds of: 5轮:

  • 15 Pull Ups 15拉上
  • 30 Squats 30蹲
  • 200m Run 200米跑

I then want the database to create the proper relationship between the workout instance and the movement models that are used in the workout instance's TextField: Pull Up, Squat, and Run. 然后,我希望数据库在锻炼实例和锻炼实例的TextField:Pull Up,Squat和Run中使用的运动模型之间创建正确的关系。

This way I can search the database by workouts that contain pull ups, squats, or runs and this workout will show up. 通过这种方式,我可以通过包含上拉,深蹲或跑步的锻炼来搜索数据库,并且此锻炼将显示出来。 Each movement model also contains a 'Classification' ForeignKey of Upper Body, Lower Body etc. So searches can also be done for workouts that are Upper body only and so on. 每个运动模型还包含ForeignKey ,下半身等的“分类”外键。因此,也可以对仅作为上半身的训练进行搜索,依此类推。

My question is, how would I implement the model relationships between Workout, Movement, and Classification? 我的问题是,我如何实施锻炼,运动和分类之间的模型关系? I don't think I can use a ForeignKey or ManytoMany relationship between a workout and movement because they would both need to be added at the same time. 我不认为我可以在workoutmovement之间使用ForeignKey或ManytoMany关系,因为它们都需要同时添加。 In other words, the user would have to type the workout into the TextField and then also add which movements are in the workout. 换句话说,用户必须在TextField中键入锻炼,然后还要添加锻炼中的运动。 I want this to be a one step process for the user. 我希望这是用户的一步过程。 They type in the workout, the database adds the relationship to which movements are in the workout based off what the user typed into the TextField. 他们输入锻炼,数据库根据用户键入TextField的内容添加锻炼中的运动关系。 I assume in order for this functionality to work a relationship must be created between the workout and movement model after the workout model is created, not during. 我假设为了使这个功能起作用,必须在锻炼和运动模型之间创建关系,而不是在锻炼模型创建之后。 However, I'm not sure how to create this functionality on the database level. 但是,我不确定如何在数据库级别创建此功能。

I'm currently using SQLite as the database as it is the default with Django. 我目前正在使用SQLite作为数据库,因为它是Django的默认设置。

Any suggestions? 有什么建议么?

With this code your users will be able to choose movement and bodyPart by themselves: 使用此代码,您的用户可以自行选择移动和bodyPart:

from django.db import models

movement_tuple = (
    ('Pulls up', 'Pulls up'),
    ('Squats', 'Squats'),
    ('Run', 'Run')
)

body_part = (
    ('Upper Body', 'Upper Body'),
    ('Lower Body', 'Lower Body')
)

class Workout(models.Model):
    name = models.CharField(max_length=50, default=None)
    movement = models.CharField(max_length=30, choices=movement_tuple)
    body_part = models.CharField(max_length=30, choices=body_part)
    value = models.CharField(max_length=30)

    def __str__(self):
        return(self.name)

tuples are to make choice possible 元组是为了做出选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM