I am new to stackoverflow, python and Django, have a question on the '(NOT NULL constraint failed: sixerrapp_gig.user_id)' error I am getting when i click submit on the form.
Pretty sure it is because I have not assigned a user_id value from what I have found on StackOverflow thus far, but I am not sure how to do it using the django CreateView.
The project exercise is based on a clone of fiverr.com.
Please see code below:
in models.py:
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.urlresolvers import reverse #for model forms
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.CharField(max_length=500)
about = models.CharField(max_length=1000)
def __str__(self):
return self.user.username
class Gig(models.Model):
CATEGORY_CHOICES = (
("GD", "Graphics & Design"),
("DM", "Digital & Marketing"),
("VA", "Video & Animation"),
("MA", "Music & Audio"),
("PT", "Programming & Tech")
)
title = models.CharField(max_length=500)
category = models.CharField(max_length = 2, choices=CATEGORY_CHOICES)
description = models.CharField(max_length=1000)
price = models.IntegerField(default=6)
photo = models.FileField(upload_to='gigs')
status = models.BooleanField(default=True)
user = models.ForeignKey(User)
create_time = models.DateTimeField(default=timezone.now)
def get_absolute_url(self):
return reverse('my_gigs')
def __str__(self):
return self.title
views.py:
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Gig
from .forms import GigForm
class CreateGig(CreateView):
model = Gig
fields = ['title','category','description','price','photo','status']
gig_form.html
{% extends 'base.html' %}
{% load staticfiles %}
{% block page %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'sixerrapp/form-template.html' %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock %}
What i think i need is for the user field to get a value automatically when the form is submitted. Not sure what I am missing in my code to do that.
Thanks in advance for the help!