MultipleObjectsReturned- get() returned more than one mPurchase — it returned 2
I am beginner to django. While trying to save mPurchase form I am getting MultipleObjectReturned error. I am trying to get data from form using POST request. Every time I submit the form I am getting an error.
views.py
def milkPurchase(request):
title='Buy Milk'
milk = mPurchase.objects.all()
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = get_object_or_404(mPurchase)
m.mPurchase_date=timezone.now()
m.save()
return redirect('milk-purchase')
else:
form=mPurchaseForm()
context = {
'title': title,
'form': form,
'milk':milk
}
return render(request,'dairyapp/milk-purchase.html',context)
forms.py
class mPurchaseForm(forms.ModelForm):
"""
This form is for milk purchase
"""
seller=forms.CharField(
label='Seller Name',
max_length=50,
)
mPurchase_product=forms.ChoiceField(
choices=MILK_CHOICES,
label='Milk Type',
initial='',
widget=forms.Select(),
required=True
)
mPurchase_qty=forms.FloatField(
label='Qty'
)
mPurchase_rate=forms.FloatField(
label='Rate'
)
class Meta:
model=mPurchase
fields=('seller','mPurchase_product','mPurchase_qty','mPurchase_rate',)
here is my
models.py
class mPurchase(models.Model):
mPurchase_id=models.AutoField(primary_key=True)
seller=models.CharField(max_length=50)
mPurchase_date=models.DateTimeField(default=timezone.now)
mPurchase_product=models.CharField(max_length=10,choices=MILK_CHOICES)
mPurchase_qty=models.FloatField()
mPurchase_rate=models.FloatField()
def __str__(self):
return self.seller
Can you please help me get through this? I have tried using
mPurchase.objects.filter()
Also, tried to catch exception using ObjectDoesNotExist and MultipleObjectReturned
from django.core.exceptions import ObjectDoesNotExist,
MultipleObjectsReturned
django python-3.x django-models django-forms django-views
add a comment |
I am beginner to django. While trying to save mPurchase form I am getting MultipleObjectReturned error. I am trying to get data from form using POST request. Every time I submit the form I am getting an error.
views.py
def milkPurchase(request):
title='Buy Milk'
milk = mPurchase.objects.all()
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = get_object_or_404(mPurchase)
m.mPurchase_date=timezone.now()
m.save()
return redirect('milk-purchase')
else:
form=mPurchaseForm()
context = {
'title': title,
'form': form,
'milk':milk
}
return render(request,'dairyapp/milk-purchase.html',context)
forms.py
class mPurchaseForm(forms.ModelForm):
"""
This form is for milk purchase
"""
seller=forms.CharField(
label='Seller Name',
max_length=50,
)
mPurchase_product=forms.ChoiceField(
choices=MILK_CHOICES,
label='Milk Type',
initial='',
widget=forms.Select(),
required=True
)
mPurchase_qty=forms.FloatField(
label='Qty'
)
mPurchase_rate=forms.FloatField(
label='Rate'
)
class Meta:
model=mPurchase
fields=('seller','mPurchase_product','mPurchase_qty','mPurchase_rate',)
here is my
models.py
class mPurchase(models.Model):
mPurchase_id=models.AutoField(primary_key=True)
seller=models.CharField(max_length=50)
mPurchase_date=models.DateTimeField(default=timezone.now)
mPurchase_product=models.CharField(max_length=10,choices=MILK_CHOICES)
mPurchase_qty=models.FloatField()
mPurchase_rate=models.FloatField()
def __str__(self):
return self.seller
Can you please help me get through this? I have tried using
mPurchase.objects.filter()
Also, tried to catch exception using ObjectDoesNotExist and MultipleObjectReturned
from django.core.exceptions import ObjectDoesNotExist,
MultipleObjectsReturned
django python-3.x django-models django-forms django-views
add a comment |
I am beginner to django. While trying to save mPurchase form I am getting MultipleObjectReturned error. I am trying to get data from form using POST request. Every time I submit the form I am getting an error.
views.py
def milkPurchase(request):
title='Buy Milk'
milk = mPurchase.objects.all()
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = get_object_or_404(mPurchase)
m.mPurchase_date=timezone.now()
m.save()
return redirect('milk-purchase')
else:
form=mPurchaseForm()
context = {
'title': title,
'form': form,
'milk':milk
}
return render(request,'dairyapp/milk-purchase.html',context)
forms.py
class mPurchaseForm(forms.ModelForm):
"""
This form is for milk purchase
"""
seller=forms.CharField(
label='Seller Name',
max_length=50,
)
mPurchase_product=forms.ChoiceField(
choices=MILK_CHOICES,
label='Milk Type',
initial='',
widget=forms.Select(),
required=True
)
mPurchase_qty=forms.FloatField(
label='Qty'
)
mPurchase_rate=forms.FloatField(
label='Rate'
)
class Meta:
model=mPurchase
fields=('seller','mPurchase_product','mPurchase_qty','mPurchase_rate',)
here is my
models.py
class mPurchase(models.Model):
mPurchase_id=models.AutoField(primary_key=True)
seller=models.CharField(max_length=50)
mPurchase_date=models.DateTimeField(default=timezone.now)
mPurchase_product=models.CharField(max_length=10,choices=MILK_CHOICES)
mPurchase_qty=models.FloatField()
mPurchase_rate=models.FloatField()
def __str__(self):
return self.seller
Can you please help me get through this? I have tried using
mPurchase.objects.filter()
Also, tried to catch exception using ObjectDoesNotExist and MultipleObjectReturned
from django.core.exceptions import ObjectDoesNotExist,
MultipleObjectsReturned
django python-3.x django-models django-forms django-views
I am beginner to django. While trying to save mPurchase form I am getting MultipleObjectReturned error. I am trying to get data from form using POST request. Every time I submit the form I am getting an error.
views.py
def milkPurchase(request):
title='Buy Milk'
milk = mPurchase.objects.all()
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = get_object_or_404(mPurchase)
m.mPurchase_date=timezone.now()
m.save()
return redirect('milk-purchase')
else:
form=mPurchaseForm()
context = {
'title': title,
'form': form,
'milk':milk
}
return render(request,'dairyapp/milk-purchase.html',context)
forms.py
class mPurchaseForm(forms.ModelForm):
"""
This form is for milk purchase
"""
seller=forms.CharField(
label='Seller Name',
max_length=50,
)
mPurchase_product=forms.ChoiceField(
choices=MILK_CHOICES,
label='Milk Type',
initial='',
widget=forms.Select(),
required=True
)
mPurchase_qty=forms.FloatField(
label='Qty'
)
mPurchase_rate=forms.FloatField(
label='Rate'
)
class Meta:
model=mPurchase
fields=('seller','mPurchase_product','mPurchase_qty','mPurchase_rate',)
here is my
models.py
class mPurchase(models.Model):
mPurchase_id=models.AutoField(primary_key=True)
seller=models.CharField(max_length=50)
mPurchase_date=models.DateTimeField(default=timezone.now)
mPurchase_product=models.CharField(max_length=10,choices=MILK_CHOICES)
mPurchase_qty=models.FloatField()
mPurchase_rate=models.FloatField()
def __str__(self):
return self.seller
Can you please help me get through this? I have tried using
mPurchase.objects.filter()
Also, tried to catch exception using ObjectDoesNotExist and MultipleObjectReturned
from django.core.exceptions import ObjectDoesNotExist,
MultipleObjectsReturned
django python-3.x django-models django-forms django-views
django python-3.x django-models django-forms django-views
asked Nov 22 at 18:09
anisbhsl
54
54
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to change in here:
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = form.save(commit=False) # <--- Here
m.mPurchase_date=timezone.now()
m.save()
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%2f53436285%2fmultipleobjectsreturned-get-returned-more-than-one-mpurchase-it-returned-2%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
You need to change in here:
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = form.save(commit=False) # <--- Here
m.mPurchase_date=timezone.now()
m.save()
add a comment |
You need to change in here:
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = form.save(commit=False) # <--- Here
m.mPurchase_date=timezone.now()
m.save()
add a comment |
You need to change in here:
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = form.save(commit=False) # <--- Here
m.mPurchase_date=timezone.now()
m.save()
You need to change in here:
if request.method=='POST':
form=mPurchaseForm(request.POST)
if form.is_valid():
m = form.save(commit=False) # <--- Here
m.mPurchase_date=timezone.now()
m.save()
answered Nov 22 at 18:52
ruddra
11.4k32648
11.4k32648
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53436285%2fmultipleobjectsreturned-get-returned-more-than-one-mpurchase-it-returned-2%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