1

I want to select an event and upload a photo, but when I make migrations, it gives NOT NULL constraint failed: myapp_doc.event error.

I'm getting the error even after deleting the view. What should I do ?

models

class Doc(models.Model):
    events = (
        (None, "choose one of it"),
        ('bbq', 'Barbeque '),
        ('meet', 'Meeting'),
    )
    doc = models.FileField(upload_to='uploads/')
    user = models.ForeignKey(User, null=False, blank=True)
    event = models.CharField(max_length=15, choices=events, null = True)
    def __unicode__(self):
        return unicode(self.user)

View

def upload_file(request):
    user= request.user
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
        doc = form.save(commit=False)
        doc.user = request.user
        doc.save()
        messages.success(request, 'Dosya Yuklendi')

    return HttpResponseRedirect('/uploadnew/')

return render(request, 'upload.html', {'form': form})


def upload_file(request):
    user= request.user
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
        doc = form.save(commit=False)
        doc.user = request.user
        doc.save()
        messages.success(request, 'Dosya Yuklendi')

        return HttpResponseRedirect('/uploadnew/')

return render(request, 'upload.html', {'form': form})
4
  • Where does the error happen? Can you edit your original question with traceback? When you change your model did you create a migration as well? Commented Jul 28, 2016 at 13:40
  • this has nothing to do with your view. Please delete it from the question and explain what changes you made to your model Commented Jul 28, 2016 at 13:40
  • @ShangWang I added events in models then I makemigrations when I migrate, the problem occurs. Commented Jul 28, 2016 at 13:43
  • Hey @HamlockB, if you are facing problem at the time of migration, then it is no where related to your views. But if you are getting this error when you are uploading a photo, then it will depend on the combination of your models as well as on the data that you are uploading. So when exactly are you getting that error? Moreover the error stated by you corresponds to the 'event' column of your 'doc' model and not on the 'user' column, hence the solution provided by h3X3n didn't work for you as it addressed the 'user' column and not the 'event' column. Commented Jul 28, 2016 at 14:53

1 Answer 1

1

Okay I got your problem.

In models.py, you are defining the null = True option for the event column which is of the type models.CharField.

event = models.CharField(max_length=15, choices=events, null = True)

But as a matter of fact, 'CharField' doesn't have the null option in it. Only the 'max_length' option is available.

See this -> https://docs.djangoproject.com/en/1.9/ref/models/fields/

So technically, you need to remove that null = True part and if you really want to allow the user of your web-app to have the freedom of not selecting any option, then you can add a default value to your event column which will pick a default value from the choices defined by you in the events column.

See the example in the official docs. It's the same as what you want -> https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.Field.choices

So you event column will finally look something like this, if your default value is Barbeque:

event = models.CharField(max_length=15, choices=events, default=bbq)

Also, as shown in the official docs, I don't think you need the single quotes around 'bbq' or 'meet'.

I hope this will solve your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.