简体   繁体   English

为 Django Rest 框架制作models.py 主键和外键

[英]Making models.py for Django Rest Framework Primary key and foreign key

I made a models like this:我做了一个这样的模型:

from django.db import models
import json

# Create your models here.
class Data(models.Model):
node_id = models.ForeignKey("Node", on_delete=models.CASCADE)
timestamp = models.DateTimeField()
vibration = models.IntegerField()
moisture = models.IntegerField()
gps_latitude = models.CharField(max_length=250)
gps_longitude = models.CharField(max_length=250)
gyro_x = models.FloatField()
gyro_y = models.FloatField()
gyro_z = models.FloatField()
accelero_x = models.FloatField()
accelero_y = models.FloatField()
accelero_z = models.FloatField()
displacement = models.IntegerField()


class Node(models.Model):
node_id = models.IntegerField()
latitude = models.CharField(max_length=250)
longitude = models.CharField(max_length=250)
lokasi = models.CharField(max_length=500)

The Data models have endpoint at http://127.0.0.1:8000/api/data/ And Node models have endpoint at http://127.0.0.1:8000/api/node/ I want the node_id in Node class to become a primary key and the node_id from Data class to become a foreign key. The Data models have endpoint at http://127.0.0.1:8000/api/data/ And Node models have endpoint at http://127.0.0.1:8000/api/node/ I want the node_id in Node class to become a主键和来自数据 class 的 node_id 成为外键。 I try to post to http://127.0.0.1:8000/api/node/ like this:我尝试像这样发布到http://127.0.0.1:8000/api/node/

    {
    "node_id": 1,
    "latitude": "123",
    "longitude": "123",
    "lokasi": "eer"
}

And then I open the endpoint for Data models at http://127.0.0.1:8000/api/data/ to post some data.然后我在http://127.0.0.1:8000/api/data/打开数据模型的端点以发布一些数据。 On the Node id field, it should be referring to node_id=1, but why it just referring to node object(8) which is the id that generates automatically from Django rest framework?在 Node id 字段上,它应该指的是 node_id=1,但为什么它只是指 node object(8),它是从 Django rest 框架自动生成的 id? API 数据模型

It makes the post data look like this:它使发布数据看起来像这样:

{
"id": 13,
"timestamp": "2020-04-14T20:00:00+07:00",
"vibration": 1,
"moisture": 1,
"gps_latitude": "123",
"gps_longitude": "123",
"gyro_x": 1.0,
"gyro_y": 1.0,
"gyro_z": 1.0,
"accelero_x": 1.0,
"accelero_y": 1.0,
"accelero_z": 1.0,
"displacement": 1,
"node_id": 8}

The "node_id": should be 1 not 8. Is there any wrong with my models? “node_id”:应该是 1 而不是 8。我的模型有什么问题吗?

Depending on the relation, which I supposed is 1:N (Node:Data)根据关系,我认为是 1:N (Node:Data)

So if you'd rewrite your models to:因此,如果您将模型重写为:

class Node(models.Model):
    latitude = models.CharField(max_length=250)
    longitude = models.CharField(max_length=250)
    lokasi = models.CharField(max_length=500)

    def __str__(self):
        return self.lokasi


class Data(models.Model):
    node_id = models.ForeignKey(Node, related_name="data", on_delete=models.CASCADE)
    timestamp = models.DateTimeField()
    vibration = models.IntegerField()
    moisture = models.IntegerField()
    gps_latitude = models.CharField(max_length=250)
    gps_longitude = models.CharField(max_length=250)
    gyro_x = models.FloatField()
    gyro_y = models.FloatField()
    gyro_z = models.FloatField()
    accelero_x = models.FloatField()
    accelero_y = models.FloatField()
    accelero_z = models.FloatField()
    displacement = models.IntegerField()

So now you can assign Node by id when creating Data model, and vice versa you can assign collection of data objects from Node by calling因此,现在您可以在创建数据 model 时按 id 分配节点,反之亦然,您可以通过调用从节点分配数据对象的集合

Node.objects.first().data.all()

Ad: why it just referring to node object(8)广告:为什么它只是指节点对象(8)

You need to add a __str__ method which will tell Django how to interpret your object when casted to string, so I added a example into your Node class您需要添加一个__str__方法,该方法将告诉 Django 在转换为字符串时如何解释您的 object,因此我在您的节点 class 中添加了一个示例

Node will have id field as Autogenerated Primary key and node_id as ForeignKey to Node节点将具有id字段作为自动生成的主键和node_id作为Node的外键

Since you are trying to design the back end for a GPS system, I would recommend checking https://github.com/openwisp/django-rest-framework-gis &https://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/ first & having a solid grasp on Django models & query API.由于您正在尝试为 GPS 系统设计后端,我建议您检查https://github.com/openwisp/django-rest-framework-gis &Z5E056C500A1C4B6A7110//docs.BadejangoD807 ref/contrib/gis/tutorial/首先并牢牢掌握 Django 模型和查询 API。 this is not exactly a fix for your question but more of a high-level suggestion after checking your codes and other related comments on the thread.这不完全是解决您的问题的方法,而是在检查您的代码和线程上的其他相关评论后提供的更多高级建议。 These will help you develop GIS API more easily.这些将帮助您更轻松地开发 GIS API。

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

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