IntegrityError at /products/new/ NOT NULL constraint failed: products_product.user_id
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
add a comment |
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
You forgot to add a user to your model.new_product.user = user
before younew_product.save()
.
– Burhan Khalid
Nov 19 '18 at 2:33
add a comment |
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
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
django
asked Nov 18 '18 at 19:17
lucbranlucbran
166
166
You forgot to add a user to your model.new_product.user = user
before younew_product.save()
.
– Burhan Khalid
Nov 19 '18 at 2:33
add a comment |
You forgot to add a user to your model.new_product.user = user
before younew_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
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You forgot to add a user to your model.
new_product.user = user
before younew_product.save()
.– Burhan Khalid
Nov 19 '18 at 2:33