Ismail,
You don't seem to be handling the value for Product.slug anywhere, either in the model or the view.
You can fix the slug issue by overriding the Product model's save () method like this:
def save (self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Product, self).save (*args, **kwargs)
This way, you can automatically generate the value of the slug field when a valid form is submitted.
If you are still having problems, please, do let us know.
All the best.
Sincerely,
Muhammad
On May 25, 2017 7:20 AM, "Ismail Sarenkapic" <ismailgim96@gmail.com> wrote:
Hi, I'm trying to create a digital marketplace site where users will be able to register and submit their products.--
I created the Product model and connected it with User with foreign key.Everything seems to be alright, but when I submit my product i get the following error:
null value in column violates not-null constraint
it looks like relation between product and user is not properly configured(regardless of my mixin that is suppose to handle it)____________________________________________________________ ______________________________ ______________________________ __________ note that I'm using:database:postgresqlallauth(for user registration)____________________________________________________________ ______________________________ ______________________________ __________
________models.pyfrom django.conf import settings_______
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.core.urlresolvers import reverse
from django.utils.text import slugify
# Create your models here.
class Product(models.Model):
seller = models.ForeignKey(settings.AUTH_USER_MODEL)
#user = models.OneToOneField(settings.AUTH_USER_MODEL) # user = models.ForeignKey(settings.AUTH_USER_MODEL) # managers = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="managers_ title = models.CharField(max_length=30products", blank=True) ) #owiuerpoajsdlfkjasd; flkiu1p3o4u134123 ewjfa;sd slug = models.SlugField(blank=True, unique=True)
description = models.TextField()
price = models.DecimalField(max_digits=100, decimal_places=2, default=9.99, null=True,) #100.00
def __unicode__(self): #def __unicode__(self):
return self.title
views.pyfrom django.views import View_______
from django.views.generic import (
CreateView,
)
from .forms import ProductModelForm
from .mixins import FormUserNeededMixin
# Create your views here.
class ProductCreateView(FormUserNeededMixin, CreateView):
form_class = ProductModelForm
template_name = 'create_view.html'
forms.pyfrom django import forms________
from django.utils.text import slugify
from .models import Product
class ProductModelForm(forms.ModelForm):
class Meta:
model = Product
fields = [
"title",
"description",
"price",
]
mixins.pyfrom django import forms
from django.forms.utils import ErrorList
class FormUserNeededMixin(object):
def form_valid(self, form):
if self.request.user.is_authenticated():
form.instance.user = self.request.user
return super(FormUserNeededMixin, self).form_valid(form)
else:
form._errors[forms.forms.NON_FIELD_ERRORS] = ErrorList(["User must be logged in to continue."])
return self.form_invalid(form)
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com .
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users .
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0d2d44eb- .a2df-4f00-8d60-3f163ed72c9b% 40googlegroups.com
For more options, visit https://groups.google.com/d/optout .
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJOFuZwG1w%2B9-c3xP9aX3D5ggNv6FymU%3DG7nBOaHSjCgGwGJ1A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment