django - NOT NULL constraint failed error -


i want select event , upload photo, when make migrations, gives not null constraint failed: myapp_doc.event error.

i'm getting error after deleting view. should ?

models

class doc(models.model):     events = (         (none, "choose 1 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}) 

okay got problem.

in models.py, defining null = true option event column of type models.charfield.

event = models.charfield(max_length=15, choices=events, null = true) 

but matter of fact, 'charfield' doesn't have null option in it. 'max_length' option available.

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

so technically, need remove null = true part , if want allow user of web-app have freedom of not selecting option, can add default value event column pick default value choices defined in events column.

see example in official docs. it's same want -> https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.field.choices

so event column this, if default value barbeque:

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

also, shown in official docs, don't think need single quotes around 'bbq' or 'meet'.

i hope solve problem.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -