Django Tastypie REST上的外键-型号'';具有空属性
我需要列出每个员工的工作时间,但我得到: 模型“”具有空属性“work\u Travely”,不允许空值。 关于: /rest/tastype/employee/?format=json models.pyDjango Tastypie REST上的外键-型号'';具有空属性,django,rest,tastypie,Django,Rest,Tastypie,我需要列出每个员工的工作时间,但我得到: 模型“”具有空属性“work\u Travely”,不允许空值。 关于: /rest/tastype/employee/?format=json models.py class Employee(): registration = models.CharField(u'Registration', max_length=20, unique=True) work_journey = models.ForeignKey(WorkJourne
class Employee():
registration = models.CharField(u'Registration', max_length=20, unique=True)
work_journey = models.ForeignKey(WorkJourney, null=True, blank=True)
hr.models.py
class WorkJourney(ModelPlus):
code = models.CharField(max_length=10, null=True, unique=True)
workinghours = models.CharField(max_length=40)
excluded = models.BooleanField()
class Meta:
db_table='work_journey'
verbose_name = u'Work Journey'
def __unicode__(self):
return self.workinghours
资源.py
from suap.models import Employee
from hr.models import WorkJourney
class WorkJourneyResource(ModelResource):
class Meta:
queryset = WorkJourney.objects.all()
resource_name = 'work_journey'
authentication = BasicAuthentication()
class EmployeeResource(ModelResource):
journey = fields.ForeignKey(WorkJourney, 'work_journey')
class Meta:
queryset = Employee.objects.all()
resource_name = 'employee'
authentication = BasicAuthentication()
1/当您在ressoure.py中定义关系时,您需要一个
工作行程资源
而不是工作行程
2/要允许空值,只需添加null=True,blank=True
以下是修复代码:
class EmployeeResource(ModelResource):
journey = fields.ForeignKey(WorkJourneyResource, 'work_journey', null=True, blank=True)
....
我的WorkJourneyResource不好,我的代码还可以。在添加null=True、blank=True时,它可以工作。但是它只是从工作之旅返回资源uri。这是一种正常的行为。在您的字段中添加
full=True