IntegrityError at /products/new/ NOT NULL constraint failed: products_product.user_id












0















I am creating a marketplace where selected users can have their shop and CRUD their products.
I am running an error when creating the Createview class. I need the form where user adds a new product, returns his own shop name based on the shop model, but looks like there is an error as above.
Following is my app:



models.py



class Shop(models.Model):

shop_name = models.CharField(max_length=120)
owner = models.OneToOneField(User,on_delete=models.CASCADE, related_name="owner")

def __str__(self):
return self.shop_name

class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)


views.py



class ProductCreateView(LoginRequiredMixin,SubmitBtnMixin, CreateView):
model = Product
form_class = ProductForm
template_name = 'form.html'
success_url = '/products/list'
submit_btn = 'Add Product'

def form_valid(self, form):
new_product = form.save(commit=False)
user = self.request.user
s = Shop.objects.get (owner=user)
new_product.shop = s
new_product.save()
return super (ProductCreateView, self).form_valid(form)


FORM.PY



class ProductForm(ModelForm):

class Meta:
model = Product
fields = ['category', 'title', 'description', 'price', 'image']
exclude = ['shop']









share|improve this question























  • You forgot to add a user to your model. new_product.user = user before you new_product.save().

    – Burhan Khalid
    Nov 19 '18 at 2:33
















0















I am creating a marketplace where selected users can have their shop and CRUD their products.
I am running an error when creating the Createview class. I need the form where user adds a new product, returns his own shop name based on the shop model, but looks like there is an error as above.
Following is my app:



models.py



class Shop(models.Model):

shop_name = models.CharField(max_length=120)
owner = models.OneToOneField(User,on_delete=models.CASCADE, related_name="owner")

def __str__(self):
return self.shop_name

class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)


views.py



class ProductCreateView(LoginRequiredMixin,SubmitBtnMixin, CreateView):
model = Product
form_class = ProductForm
template_name = 'form.html'
success_url = '/products/list'
submit_btn = 'Add Product'

def form_valid(self, form):
new_product = form.save(commit=False)
user = self.request.user
s = Shop.objects.get (owner=user)
new_product.shop = s
new_product.save()
return super (ProductCreateView, self).form_valid(form)


FORM.PY



class ProductForm(ModelForm):

class Meta:
model = Product
fields = ['category', 'title', 'description', 'price', 'image']
exclude = ['shop']









share|improve this question























  • You forgot to add a user to your model. new_product.user = user before you new_product.save().

    – Burhan Khalid
    Nov 19 '18 at 2:33














0












0








0








I am creating a marketplace where selected users can have their shop and CRUD their products.
I am running an error when creating the Createview class. I need the form where user adds a new product, returns his own shop name based on the shop model, but looks like there is an error as above.
Following is my app:



models.py



class Shop(models.Model):

shop_name = models.CharField(max_length=120)
owner = models.OneToOneField(User,on_delete=models.CASCADE, related_name="owner")

def __str__(self):
return self.shop_name

class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)


views.py



class ProductCreateView(LoginRequiredMixin,SubmitBtnMixin, CreateView):
model = Product
form_class = ProductForm
template_name = 'form.html'
success_url = '/products/list'
submit_btn = 'Add Product'

def form_valid(self, form):
new_product = form.save(commit=False)
user = self.request.user
s = Shop.objects.get (owner=user)
new_product.shop = s
new_product.save()
return super (ProductCreateView, self).form_valid(form)


FORM.PY



class ProductForm(ModelForm):

class Meta:
model = Product
fields = ['category', 'title', 'description', 'price', 'image']
exclude = ['shop']









share|improve this question














I am creating a marketplace where selected users can have their shop and CRUD their products.
I am running an error when creating the Createview class. I need the form where user adds a new product, returns his own shop name based on the shop model, but looks like there is an error as above.
Following is my app:



models.py



class Shop(models.Model):

shop_name = models.CharField(max_length=120)
owner = models.OneToOneField(User,on_delete=models.CASCADE, related_name="owner")

def __str__(self):
return self.shop_name

class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)


views.py



class ProductCreateView(LoginRequiredMixin,SubmitBtnMixin, CreateView):
model = Product
form_class = ProductForm
template_name = 'form.html'
success_url = '/products/list'
submit_btn = 'Add Product'

def form_valid(self, form):
new_product = form.save(commit=False)
user = self.request.user
s = Shop.objects.get (owner=user)
new_product.shop = s
new_product.save()
return super (ProductCreateView, self).form_valid(form)


FORM.PY



class ProductForm(ModelForm):

class Meta:
model = Product
fields = ['category', 'title', 'description', 'price', 'image']
exclude = ['shop']






django






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 19:17









lucbranlucbran

166




166













  • You forgot to add a user to your model. new_product.user = user before you new_product.save().

    – Burhan Khalid
    Nov 19 '18 at 2:33



















  • You forgot to add a user to your model. new_product.user = user before you new_product.save().

    – Burhan Khalid
    Nov 19 '18 at 2:33

















You forgot to add a user to your model. new_product.user = user before you new_product.save().

– Burhan Khalid
Nov 19 '18 at 2:33





You forgot to add a user to your model. new_product.user = user before you new_product.save().

– Burhan Khalid
Nov 19 '18 at 2:33












1 Answer
1






active

oldest

votes


















1














In your Product model I don't see an attribute 'description', but in your form you have specified a field 'description'. Try adding null=True, blank=True to the user field in your Product model.



class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)





share|improve this answer


























  • Hi, yeah pasting I truncated some of my model fields as the issue was surely in the first rows. After your suggestion now it works..will this change (null=True, blank=True ) affect the user model function in any case? thanks

    – lucbran
    Nov 19 '18 at 13:47













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364569%2fintegrityerror-at-products-new-not-null-constraint-failed-products-product-us%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In your Product model I don't see an attribute 'description', but in your form you have specified a field 'description'. Try adding null=True, blank=True to the user field in your Product model.



class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)





share|improve this answer


























  • Hi, yeah pasting I truncated some of my model fields as the issue was surely in the first rows. After your suggestion now it works..will this change (null=True, blank=True ) affect the user model function in any case? thanks

    – lucbran
    Nov 19 '18 at 13:47


















1














In your Product model I don't see an attribute 'description', but in your form you have specified a field 'description'. Try adding null=True, blank=True to the user field in your Product model.



class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)





share|improve this answer


























  • Hi, yeah pasting I truncated some of my model fields as the issue was surely in the first rows. After your suggestion now it works..will this change (null=True, blank=True ) affect the user model function in any case? thanks

    – lucbran
    Nov 19 '18 at 13:47
















1












1








1







In your Product model I don't see an attribute 'description', but in your form you have specified a field 'description'. Try adding null=True, blank=True to the user field in your Product model.



class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)





share|improve this answer















In your Product model I don't see an attribute 'description', but in your form you have specified a field 'description'. Try adding null=True, blank=True to the user field in your Product model.



class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
shop = models.ForeignKey (Shop, on_delete=models.CASCADE, related_name='shop')
category = models.ForeignKey(Category, verbose_name ='Categoria',on_delete=False )
title = models.CharField(max_length=120)
slug = models.SlugField(blank= True, null=True, unique = True)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 2:22

























answered Nov 19 '18 at 2:15









WhodiniWhodini

50913




50913













  • Hi, yeah pasting I truncated some of my model fields as the issue was surely in the first rows. After your suggestion now it works..will this change (null=True, blank=True ) affect the user model function in any case? thanks

    – lucbran
    Nov 19 '18 at 13:47





















  • Hi, yeah pasting I truncated some of my model fields as the issue was surely in the first rows. After your suggestion now it works..will this change (null=True, blank=True ) affect the user model function in any case? thanks

    – lucbran
    Nov 19 '18 at 13:47



















Hi, yeah pasting I truncated some of my model fields as the issue was surely in the first rows. After your suggestion now it works..will this change (null=True, blank=True ) affect the user model function in any case? thanks

– lucbran
Nov 19 '18 at 13:47







Hi, yeah pasting I truncated some of my model fields as the issue was surely in the first rows. After your suggestion now it works..will this change (null=True, blank=True ) affect the user model function in any case? thanks

– lucbran
Nov 19 '18 at 13:47




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364569%2fintegrityerror-at-products-new-not-null-constraint-failed-products-product-us%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)