user_id may not be NULL
I'm serving my Django models via TastyPie and when attempting to POST a new Booking model, I keep getting this error:
error_message: "bookings_booking.user_id may not be NULL"
Even using curl produces it:
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"date": "2011-08-17", "time": "17:30", "duration": 30, "description": "meh", "user": "/api/v1/user/1/", "room": "/api/room/1/"}' http://localhost:8000/api/booking/
I have tried deleting my database and run syncdb multiple times already but the problem persists.
Here are my Django models:
class User(models.Model):
login = models.CharField('login', max_length=12)
password = models.CharField('password', max_length=12)
fullname = models.CharField('fullname', max_length=30)
class Room(models.Model):
name = models.CharField('room name', max_length=30)
code = models.CharField('room code', max_length=4)
class Booking(models.Model):
date = models.DateField('date booked')
time = models.TimeField('time booked')
duration = models.PositiveSmallIntegerField('duration booked') #x * 15
user = models.ForeignKey(User, related_name='bookings')
room = models.ForeignKey(Room, related_name='bookings')
description = models.TextField()
And here's my Tastypie resources:
class RoomResource(ModelResource):
class Meta:
queryset = Room.objects.all()
resource_name = 'room'
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
class BookingResource(ModelResource):
room = fields.ForeignKey(RoomResource, 'room')
class Meta:
queryset = Booking.objects开发者_运维百科.all()
resource_name = 'booking'
filtering = {
"room" : ALL_WITH_RELATIONS,
"date" : ['exact', 'range'],
"time" : ['gte', 'lte']
}
authorization = Authorization()
I believe you need to add a user FK to your BookingResource
. Like So::
class BookingResource(ModelResource):
room = fields.ForeignKey(RoomResource, 'room')
user = fields.ForeignKey(UserREsource, 'user')
class Meta:
queryset = Booking.objects.all()
resource_name = 'booking'
filtering = {
"room" : ALL_WITH_RELATIONS,
"date" : ['exact', 'range'],
"time" : ['gte', 'lte']
}
authorization = Authorization()
精彩评论