I have a ModelForm where i give users a chance to create a new or edit an existing "Group" object. If "Edit" an existing is requested, i have it pre-populating the form data with the existing information. When the user goes to "Save" an edit on an existing object, i get this error:
"Group with this Name already exists."
How can i tell Django to "Update" the object instead of attempting to create a new one with the same name?
MODEL
class Analyst(models.Model):
first = models.CharField(max_length=32)
last = models.CharField(max_length=32)
def __unicode__(self):
return "%s, %s" % (self.last, self.first)
class Alias(models.Model):
alias = models.CharField(max_length=32)
def __unicode__(self):
return "%s" % (self.alias)
class Octet(models.Model):
num = models.IntegerField(max_length=3)
def __unicode__(self):
return "%s" % (self.num)
class Group(models.Model):
name = models.CharField(max_length=32, unique=True) #name of the group
octets = models.ManyToManyField(Octet, blank=True) #not required
aliases = models.ManyToManyField(Alias, blank=True) #not required
analyst = models.ForeignKey(Analyst) #analyst assigned to group, required
def __unicode__(self):
return "%s" % (self.name)
VIEW
class GroupEditForm(ModelForm):
class Meta:
model = Group
def index(request):
if request.method == 'GET':
groups = Group.objects.all().order_by('name')
return render_to_response('groups.html',
{ 'groups': groups, },
context_instance = RequestContext(request),
)
def edit(request):
if request.method == "POST":
form = GroupEditForm(instance = Group.objects.get(name=request.POST['name']))
elif request.method == "GET":
form = GroupEditForm()
return render_to_response('group_edit.html',
{ 'form': form, },
context_instance = RequestContext(request),
)
def save(request):
if request.method == "POST":
form = GroupEditForm(request.POST)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect('/groups/')
return render_to_response('group_save.html',
{ 'test': form.errors, })
You have to get the extining group object in the save action and bound the form to it.
def save(request, id):
if request.method == "POST":
group = get_object_or_404(Group, pk=id)
form = GroupEditForm(request.POST, instance=group) # A form bound to the POST data
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect('/groups/')
return render_to_response('group_save.html',
{ 'test': form.errors, })
Related
I have these models:
class Customers(models.Model):
ID = models.AutoField(primary_key=True)
...
def __str__(self):
return str(self.ID)
class CustomerAddresses(models.Model):
ID = models.AutoField(primary_key=True)
...
CustomerNoID = models.ForeignKey('Customers', on_delete=models.CASCADE)
def __str__(self):
return str(self.ID)
and my view:
def add_customer_view(request):
user_id = request.user.id
last_customerno = Customers.objects.filter(UserID=user_id).order_by('CustomerNo').last()
if not last_customerno:
# return '0001'
last_customerno = 1000
if last_customerno == 1000:
customerno_int = 1000
else:
customerno_int = last_customerno.CustomerNo + 1
# if this is a POST request we need to process the form data
if request.method == 'POST':
customer_form = CustomerForm(request.user.id, request.POST)
customer_address_form = CustomerAddressesForm(request.user.id, request.POST)
if customer_form.is_valid():
new_customer = customer_form.save(commit=False)
new_customer.save()
if customer_address_form.is_valid():
new_address = customer_address_form.save(commit=False)
new_address.CustomerNoID = new_customer
new_address.save()
return HttpResponseRedirect('/backend/kunder/')
else:
customer_form = CustomerForm(request.user.id, initial={'CustomerNo': customerno_int})
customer_address_form = CustomerAddressesForm(request.user.id)
return render(
request,
'backend/add_customer.html',
{
'title': 'WestcoastShop - Backend',
'customer_form': customer_form,
'customer_address_form': customer_address_form
}
)
But just the Customer is creating not the address I think the form is missing the CustomerNoID and I think I got the right way but after 6 hrs I give up maybe here is a smart guy how finds the error.
regards.
I changed the form and added the second one in a modal so I can save the two models nondependent.
Hey guys how can i set initial value in my form field, let say the user click "BidForm" in the search form, i want the BidForm value will be the value of ProjectName in the other form...
here's my code in my search views
def search_views(request):
project_list = ProjectNameInviToBid.objects.all()
query = request.GET.get('query')
if query:
project_list = project_list.filter(ProjectName__icontains=query)
context = {
'project_list': project_list
}
return render(request, 'content/search_views.html', context)
and my other views
def project_name_details(request, sid):
majordetails = ProjectNameInviToBid.objects.get(id=sid)
if request.method == 'POST':
form = invitoBidForm(request.POST, request.FILES)
form.fields['ProjectName'].initial = majordetails
if form.is_valid():
form.save()
messages.success(request, 'File has been Uploaded')
else:
form = invitoBidForm()
args = {
'majordetails': majordetails,
'form': form
}
return render(request,'content/invitoBid/bacadmininvitoBid.html', args)
my form.py
class invitoBidForm(ModelForm):
class Meta:
model = InviToBid
fields = ('ProjectName','NameOfFile', 'Contract_No', 'Bid_Opening',
'Pre_Bid_Conference', 'Non_Refundable_Bidder_Fee',
'Delivery_Period',
'Pdf_fileinvi',)
and my models.py
class ProjectNameInviToBid(models.Model):
ProjectName = models.CharField(max_length=255, verbose_name='Project Name', null=True)
DateCreated = models.DateField(auto_now=True)
def __str__(self):
return self.ProjectName
class InviToBid(models.Model):
today = date.today()
ProjectName = models.ForeignKey('ProjectNameInviToBid', on_delete=models.CASCADE)
NameOfFile = models.CharField(max_length=255, verbose_name='Name of File')
Contract_No = models.IntegerField(verbose_name='Contract No')
def __str__(self):
return self.NameOfFile
First, I shall praise your documentation. Most people fail to provide the important code.
You can add something like this to your code here that will do what you require.
An example from my own code
if request.method == 'GET' and request.user.is_authenticated:
study = Study.objects.get(pk=studyID)
form = ContactForm(initial={'from_email': request.user.email, 'subject': "Study: " + study.name ,'message': study_message.format(request.user.get_short_name(), request.user.get_full_name())})
How you should change your code
Change your code in your other views from this:
else:
form = invitoBidForm()
to
else:
form = invitoBidForm(initial={'ProjectName': <wherever your project name comes from>})
I am not able to display the invoice number in the receipt.html.
invoice number is auto generated in models.py
models.py
from random import randint
class Buyer(models.Model):
name_of_buyer = models.CharField(max_length=200,null=True)
address_of_buyer = models.CharField(max_length=200,null=True)
interested_in = models.ForeignKey(Box,on_delete=models.CASCADE,null=True)
Pickup_dt = models.DateField(null=True)
Pickup_time = models.CharField(max_length=80,null=True)
Invoice_number = models.CharField(max_length=12,blank=True,unique=True)
def save(self, *args, **kwargs):
if self.interested_in == 'Mangos':
x=randint(99,99999)
self.Invoice_number = str('MAN') + str(x)
elif self.service == 'Banana':
x=randint(99,99999)
self.Invoice_number = str('BAN') + str(x)
elif self.service == 'Apple':
x=randint(99,99999)
self.Invoice_number = str('APP') + str(x)
super(imfc_one,self).save()
def __str__(self):
return str(self.Invoice_number)
forms.py
class Sale(forms.ModelForm):
def clean_interested_in(self):
buyer_interested_in_box = self.cleaned_data['interested_in']
if buyer_interested_in_box.mango < 10:
raise forms.ValidationError('Not enough fruits.Please select another box')
class Meta:
model = Buyer
fields = '__all__'
view.py
def ind(request):
form = Sale()
if request.method == 'POST':
form = Sale(request.POST)
if form.is_valid():
form.save(commit=True)
return render(request,'app_one/receipt.html',{'upform':form.cleaned_data})
else:
print("form is not vaalid")
return render(request,'app_one/index1.html',{'form':form})
receipt.html
receipt : {{ upform.Invoice_number }}
How can I have the invoice number in the receipt.html
Thank you.
You don't have to pass form.cleaned_data in view. Just pass your saved object
def ind(request):
form = Sale()
if request.method == 'POST':
form = Sale(request.POST)
if form.is_valid():
buyer = form.save(commit=True)
return render(request,'app_one/receipt.html',{'buyer': buyer})
else:
print("form is not vaalid")
return render(request,'app_one/index1.html',{'form':form})
In receipt.html, just call
receipt : {{ buyer.Invoice_number }}
My views allows me now to see if I have two questions, it shows me 2 forms. I wish I could now instantiate my 2 forms with one question each. So that the user no longer has simply answer the question without selecting ...
My views. py :
def access(request, instance):
replies = Reply.objects.all()
pages = Page.objects.all()
numPages = Page.objects.get(pk=instance)
questions = Question.objects.filter(page=instance)
length_questions = len(questions)
logged_user = get_logged_user_from_request(request)
ReplyFormSet = modelformset_factory(model=Reply, form=ReplyForm, extra=length_questions, can_delete=True)
formset = ReplyFormSet(request.POST, queryset=Reply.objects.none())
if request.method == 'POST':
formset = ReplyFormSet(request.POST, queryset=Reply.objects.none())
if formset.is_valid():
new_instances = formset.save(commit=False)
for new_instance in new_instances:
new_instance.user = logged_user
new_instance.save()
return HttpResponseRedirect('/baseVisite/')
else:
messages.add_message(request, messages.INFO, 'Le formulaire est incorrecte !')
return render_to_response('polls/error.html', context_instance=RequestContext(request))
else:
formset = ReplyFormSet(queryset=Reply.objects.none())
return render_to_response('polls/access.html', {
'formset': formset,
'questions':questions,
'logged_user':logged_user,
'numPages' : numPages
})
my models.py :
class Page(models.Model):
title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Question(models.Model):
label = models.CharField(max_length=30)
page = models.ManyToManyField(Page)
def __str__(self):
return self.label
class Reply(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(Personne)
answer = models.CharField(max_length=30)
creationDate = models.DateTimeField(default=django.utils.timezone.now)
def __str__(self):
return str(self.answer)
and my forms.py :
class ReplyForm(forms.ModelForm):
class Meta:
model = Reply
exclude = ('user','creationDate')
I would like to pre-populate fields "questions" with this filter --> Question.objects.filter(page=instance) Is it possible to put a filter like this?
You can use the queryset argument
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset
I'm unable to figure out how to only call a queryset of items that belong to a specific User in the django forms.
dropoffs/models.py
class DropoffItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
dropoff = models.ForeignKey('Dropoff', null=True, blank=True)
product = models.ForeignKey(Product)
location = models.CharField(max_length=120, choices=LOCATION_CHOICES, default="Customer")
def __str__(self):
return str('%s' + " " + "(" + '%s' + ")") %(self.product.title, self.product.sku)
def sku(self):
return self.product.sku
def title(self):
return self.product.title
def dropoff_id(self):
return str(self.dropoff.id)
forms.py
class AddPickupItemForm(forms.ModelForm):
dropoffitem = forms.ModelChoiceField(queryset=DropoffItem.objects.none())
class Meta:
model = PickupItem
# fields = ["product", "quantity"]
fields = ['dropoffitem']
def __init__(self, user, *args, **kwargs):
# self.request = kwargs.pop("request")
the_user = kwargs.pop('user', None)
super(AddPickupItemForm, self).__init__(*args, **kwargs)
if the_user is not None:
self.fields["dropoffitem"].queryset = DropoffItem.objects.filter(user=the_user)
views.py
def add_item_to_pickup_order(request):
request.session.set_expiry(120000)
try:
user = request.user
the_id = request.session['pickup_id']
pickup = Pickup.objects.get(id=the_id)
except:
user = request.user
new_pickup_order = Pickup(user=user)
new_pickup_order.save()
request.session['pickup_id'] = new_pickup_order.id
the_id = new_pickup_order.id
pickup = Pickup.objects.get(id=the_id)
try:
dropoffitem = DropoffItem.objects.filter(user=user)
except DropoffItem.DoesNotExist:
pass
except:
pass
form = AddPickupItemForm(request.POST, user=request.user)
if request.method == "POST":
dropoffitem_id = int(request.POST['dropoffitem'])
pickup_item = PickupItem.objects.create(pickup=pickup, dropoffitem_id=dropoffitem_id)
pickup_item.save()
return HttpResponseRedirect('%s'%(reverse('add_item_to_pickup_order')))
context = {
"pickup": pickup,
"form": form,
}
return render(request, 'pickups/create_pickup_order.html', context)
With the modifications to init, I'm getting a TypeError of: init() got multiple values for keyword argument 'user'.
Could that be because of how I'm requesting a 'session'?
class AddPickupItemForm(ModelForm):
def __init__(self,*args,**kwargs)
the_user = kwargs.pop('user',None)
super(AddPickupItemForm, self).__init__(*args,**kwargs)
if the_user is not None:
self.fields['dropoffitem'].queryset = DropOffItem.objects.filter(user=the_user)
In other words, pass your user to the form when instantiating, if you need to.