ForeignKey on Tastypie REST - The model has an empty attribute

I need to indicate the working hours of each employee, but I get:

Model '' has an empty attribute 'work_journey' and does not allow a null value.

on the

/ leisure / tastypie / employee /? = JSON format

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() 
+4
source share
1 answer

1 / When you define your attitude in ressoure.py

you need WorkJourneyResource , not WorkJourney

2 / To enable null, just add null=True, blank=True

Here is the corrected code:

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

All Articles