不是空约束失败

时间:2017-09-21 20:27:04

标签: python django django-models

我在堆栈溢出时看了很多这些问题,但没有一个解决方案似乎对我有所帮助。我已经尝试过Null = True和Blank = True以及默认=无,它们都会出错。有人有主意吗?非常感谢!

我得到的错误是:

  

NOT NULL约束失败:first_app_trip.messages_id

我的模特:

class Trip(models.Model):
    title = models.CharField(max_length = 50)
    destination = models.CharField(max_length = 255)
    description = models.TextField()
    start_date = models.DateField(auto_now_add=False)
    end_date = models.DateField(auto_now_add=False)
    creator = models.ForeignKey(User, related_name="created_trips")
    participants = models.ManyToManyField(User, related_name="joined_trips", default=None)
    messages = models.ForeignKey(Message, related_name="messages", default=None )
    notes = models.ForeignKey(Note, related_name="notes", default=None)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now_add = True)

class Message(models.Model):
    content = models.TextField()
    author = models.ForeignKey(User, related_name="author")
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now_add = True)

class Note(models.Model):
    content = models.CharField(max_length=45)
    user = models.ForeignKey(User, related_name="notes")

我的观点:

def create(request):
    user = current_user(request)
    print user.id
    return render(request, 'first_app/create_trip.html')

def add(request):
    user = current_user(request)
    print user

    trip = Trip.objects.create(
        title = request.POST.get('title'),
        destination = request.POST.get('destination'),
        description = request.POST.get('description'),
        start_date = request.POST.get('start_date'),
        end_date = request.POST.get('end_date'),
        creator = user
        )
    print trip
    return redirect('/user_profile')

1 个答案:

答案 0 :(得分:0)

Python 区分大小写

试试吧

class Trip(models.Model):

    messages = models.ForeignKey(Message, related_name="messages", null=True, blank=True )
    # in this line     case sensitive                             ^^^^^^^^^^^^^^^^^^^

下:

./manage.py makemigrations first_app
./manage.py migrate first_app

尝试行动后