I've got the following two models. They are linked by a OneToOneField relation 'mission' going from the TextMission table to the Mission table.
#Models.py
class Mission(models.Model):
name = models.CharField(max_length = 40)
class TextMission(models.Model):
mission = models.OneToOneField(Mission, related_name="text_mission", primary_key = True)
Sometimes missions will have a corresponding TextMission, but not always. I'm able to get everything working when I'm creating/updating objects in the shell, but TastyPie only handles requests correctly when there IS a TextMission (PUTing to /mission/{id}/). When text_mission=null, it craps out.
My tastypie config:
class MissionResource(ModelResource):
text_mission = fields.ToOneField('path.to.TextMissionResource', attribute='text_mission', related_name='mission', full = True, null = True)
class TextMissionResource(ModelResource):
mission = fields.ToOneField(MissionResource, attribute='mission', related_name='text_mission')
When PUTing the following JSON back to the server (the exact same JSON I received), I get a ValueError:
FAILS
{ "name": "TEST", "id": "1", "text_mission": null, "resource_uri": "/api/web/mission/1/", } *** ValueError: Cannot assign None: "Mission.text_mission" does not allow null values.
SUCCEEDS
{ "name": "TEST", "id": "1", "text_mission": {"id": "1", "resource_uri": "/api/web/text_mission/1/"}, "resource_uri": "/api/web/mission/1/", }
Is there something I'm doing wrong in my code or it just not supposed to work this way. I'm at a loss here.
I'm running TastyPie 0.9.11.
I ended up figuring it out and it was a pretty dumb error. I just needed to add blank = True in addition to null = True:
That got everything working properly.