Django DRF:如何连接表并调用函数?

时间:2019-04-22 14:23:39

标签: django django-models django-rest-framework

我有一个使用Django Rest Framework制作的基本CRUD应用程序,该应用程序从用户那里获取交货编号,并让他们输入任何DeliveryIssue模型以简单模式形式使用的交货问题

我想添加一种方法,一旦在表单上输入了交货编号,它就可以查找Customer Name模型中出现的Delivery DateDeliveryInfo并在提交前先形成表格。然后将该信息与DeliveryIssue模型一起保存。

class DeliveryIssue(models.Model):
  class Meta:
    db_table = 'APP_Delivery_Issue'

  delivery = models.PositiveIntegerField(db_column = 'Delivery')
  issue_category = models.CharField(max_length=200, db_column = 'Issue Category')
  issue = models.CharField(max_length=200, db_column = 'Issue')
  detail = models.TextField(db_column = 'Detail')
  created_at = models.DateTimeField(auto_now_add=True, db_column = 'Create Datetime')
  updated_at = models.DateTimeField(auto_now=True, db_column = 'Update Datetime')

class DeliveryInfo(models.Model):
  detail = models.CharField(db_column='Delivery', max_length=50, blank=True, null=False, primary_key=True)
  customer = models.CharField(db_column='Customer', max_length=250, blank=True, null=True)
  delivery_date = models.DateField(db_column='Delivery Date', blank=True, null=True)

  class Meta:
      managed = False
      db_table = 'INFO_DELIVERY'

我已阅读过pre_save,但不确定如何在此处使用它。这是serializers.py文件

from rest_framework import serializers
from .models import DeliveryIssue

class DeliveryIssueSerializer(serializers.HyperlinkedModelSerializer):
  class Meta:
    model = DeliveryIssue
    fields = '__all__'

enter image description here

0 个答案:

没有答案