Django Rest Framework-外键抛出错误

时间:2019-03-05 19:56:14

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

我正在使用django rest框架,其中模型具有复合主键,其中一个是外键。

models / TestSuite.py

import { Component } from '@angular/core';
import { NgbDate } from '@ng-bootstrap/ng-bootstrap';
@Component({
  selector: 'ngbd-datepicker-popup',
  templateUrl: './datepicker-popup.html'
})
export class NgbdDatepickerPopup {
  model;

  private _secondToDate: NgbDate;


  get secondToDate() {
    return this._secondToDate;
  }
  set secondToDate(value: NgbDate) {
    value = this.sanitizeNgbDate(value);
    this._secondToDate = value;
  }

  private sanitizeNgbDate(value: NgbDate) {
    if (!value) {
      return null;
    }
    const tDate = new Date(value.year, value.month - 1, value.day);
    if (!isNaN(tDate.getTime())) {
      return value;
    } else {
      return null;
    }

  }
}

models / Team.py

class TestSuite(models.Model):
     team_name = models.ForeignKey('Team', on_delete=models.DO_NOTHING, db_column='team_name')
     suite_name = models.CharField(max_length=100)
     description = models.CharField(max_length=200, blank=True, null=True)
     schedule = models.CharField(max_length=100, blank=True, null=True)
     email_list_ok = models.CharField(max_length=200, blank=True, null=True)
     email_list_fail = models.CharField(max_length=200, blank=True, null=True)
     template_name = models.ForeignKey('EmailTemplates', on_delete=models.DO_NOTHING, db_column='template_name')

class Meta:
    managed = False
    db_table = 'test_suite'
    unique_together = (('team_name', 'suite_name'),)

TestSuiteSerializer.py

class Team(models.Model):
    team_name = models.CharField(primary_key=True, max_length=30)
    description = models.CharField(max_length=100, blank=True, null=True)

class Meta:
    managed = False
    db_table = 'team'

TestSuiteViewSet.py

class Meta:
    model = models.TestSuite
    fields = '__all__'

现在,当我执行发布请求时,它抛出以下错误

当post()在团队表中已经存在team_name时

class TestSuiteViewSet(viewsets.ModelViewSet):
    queryset = models.TestSuite.objects.all()
    serializer_class = serializers.TestSuiteSerializer

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data,
                                     many=isinstance(request.data, list))
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data,
                    status=status.HTTP_201_CREATED, headers=headers)

当post()的team表中没有team_name时

{
    "team_name": [
        "test suite with this team name already exists."
    ]
}

有人可以在这里帮助我吗?我以为我错过了什么。

1 个答案:

答案 0 :(得分:0)

外键字段的第一个参数应该是模型本身,而不是模型的字符串(例如,不是“ Team”,而是Team-对于EmailTemplate同样如此)

 class TestSuite(models.Model):
     # Change this field's first argument from a string to the Team class
     team_name = models.ForeignKey(Team, on_delete=models.DO_NOTHING, db_column='team_name')
     suite_name = models.CharField(max_length=100)
     description = models.CharField(max_length=200, blank=True, null=True)
     schedule = models.CharField(max_length=100, blank=True, null=True)
     email_list_ok = models.CharField(max_length=200, blank=True, null=True)
     email_list_fail = models.CharField(max_length=200, blank=True, null=True)
     # Change this field's first argument from a string to the EmailTemplates class
     template_name = models.ForeignKey(EmailTemplates, on_delete=models.DO_NOTHING, db_column='template_name')

     class Meta:
         managed = False
         db_table = 'test_suite'
         unique_together = (('team_name', 'suite_name'),)