简体   繁体   中英

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. I try to post to http://127.0.0.1:8000/api/node/ like this:

    {
    "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. 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? 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?

Depending on the relation, which I supposed is 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

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

Ad: why it just referring to node object(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

Node will have id field as Autogenerated Primary key and node_id as ForeignKey to 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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