2013-02-07 59 views
4

我需要列出每個員工的工作時間,但我發現:ForeignKey的上Tastypie REST - 模型「」有一個空的屬性

模型「」有一個空的屬性「work_journey」和不允許空值。

上:

/休息/ tastypie /僱員/格式= JSON

models.py

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 

resources.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() 

回答

14

1 /你需要一個WorkJourneyResource而不是WorkJourney當你定義在ressoure.py你們的關係

2 /允許空值,只需添加null=True, blank=True

這裏是代碼固定:

class EmployeeResource(ModelResource): 
    journey = fields.ForeignKey(WorkJourneyResource, 'work_journey', null=True, blank=True) 
    .... 
+0

我在WorkJourneyResource上遇到了問題,在我的代碼上沒問題。它的工作原理是null = True,blank = True。但它只是從work_journey中返回resource_uri .. –

+1

這是一個正常的行爲。在你的字段添加'full = True'.ForeignKey(..)接受我的答案,如果它的工作;-) – ablm

+0

明白了tastypie客戶端,謝謝ablm! –

相關問題