django two ModelForms with same field name on one template - python

I have two models and both have field 'status' which has different meaning for them.
class Order(models.Model):
...
status = models.PositiveIntegerField(default=0, choices=ORDER_STATUSES)
...
class ACS(models.Model):
status = models.IntegerField(default=-1, choices=STATUSES)
order = models.ForeignKey(Order, blank=True, null=True)
...
Their forms looks like:
class ACSForm(forms.ModelForm):
status = forms.ChoiceField(
choices=STATUSES,
widget=forms.Select(attrs={'class': 'form-control'})
)
...
class Meta:
model = ACS
fields = ('status',)
class OrderACSEditForm(forms.ModelForm):
status = forms.ChoiceField(
choices=ORDER_STATUSES,
widget=forms.Select(attrs={'class': 'form-control'})
)
class Meta:
model = Order
fields = ('status',)
I want to edit both this fields on the same page. My view.py looks like
def edit(request, item_id=""):
data = ACS.objects.get(pk=item_id)
form = ACSForm(instance=data)
order = Order.objects.get(id=data.order.id)
form_edit = OrderACSEditForm(instance=order)
if request.POST:
form = ACSForm(request.POST, instance=data)
form_edit = OrderACSEditForm(request.POST)
if form.is_valid() and form_edit.is_valid():
form_edit.save()
obj = form.save()
messages.add_message(request, messages.SUCCESS, 'Your data successfully saved.')
if request.POST['action'] == "save_stay":
return redirect("/panel/packages/acs/edit/" + str(obj.id))
else:
return redirect("/panel/packages/acs/")
return render(request, 'ui/packages/acs/edit.html', dict(data=data, form=form, form_edit=form_edit, item_id=item_id))
And template:
<div class="form-group {% if form.status.errors %}has-error{% endif %}">
<label>{% trans "Status" %}</label>
{% if form.status.errors %}
{% for error in form.status.errors %}
<label class="control-label">{{ error }}</label>
{% endfor %}
{% endif %}
{{ form.status }}
</div>
<div class="form-group {% if form_edit.status.errors %}has-error{% endif %}">
<label>{% trans "Order status" %}</label>
{% if form_edit.status.errors %}
{% for error in form_edit.status.errors %}
<label class="control-label">{{ error }}</label>
{% endfor %}
{% endif %}
{{ form_edit.status }}
</div>
But in result form.status gets values from form_edit.status which is not correct. I need to solve this problem without changing names of model fields but don't know how.

Use the prefix argument for your forms, to namespace the field names.
form = ACSForm(prefix='acs', instance=data)
form_edit = OrderACSEditForm(prefix='edit', instance=order)
Remember to use the same prefix when you instantiate the form with POST data as well.

Related

how to display specific results from the dropdown in models.py in django

I want to display only employees which emp_type is 'Doctor'?
** Here is Models.py **
class Employee(models.Model):
name = models.CharField(max_length=50)
emp_type_choices = [
('Nurse', 'Nurse'),
('Doctor', 'Doctor'),
('Other', 'Other'),
]
emp_type = models.CharField(
max_length=6, choices=emp_type_choices, default='Nurse')
def __str__(self):
return self.name
class Ticket(models.Model):
patient = models.CharField(max_length=50)
doctor = models.ForeignKey(Employee, on_delete=models.CASCADE)
def __str__(self):
return self.patient.name
This is my Forms.py
class TicketModelForm(forms.ModelForm):
class Meta:
model = Ticket
fields = ['doctor', 'status']
widgets = {
'doctor': forms.Select(attrs={'class': 'form-control','placeholder': 'Doctor Name'}),
}
This is my Views.py
#login_required
def TicketToGenerateView(request, pk):
ticket = get_object_or_404(Patient, pk=pk)
form = TicketModelForm(request.POST or None)
if form.is_valid():
obj.save()
return redirect('/dashboard/ticket')
context = {
'form': form,
'ticket': ticket,
}
return render(request, 'dashboard/ticket.html', context)
This is my Template
<form action="." method="POST">
{% csrf_token %}.
{% for field in form %}
<div class="form-group">
{{ field }}
{% if field.errors %}
{% for error in field.errors %}
<p class="text-danger">{{ error|escape }}</p>
{% endfor %}
{% endif %}
</div>
{% endfor %}
<div class="form-group float-right">
<button type="submit" class="btn btn-success btn-sm" value=" {{ valueBtn }} "> <span
class="glyphicon glyphicon-plus"></span> </button>
</div>
</form>
In the template, I'm displaying all registered employees as a dropdown list, but I would like to display only employees which their emp_type is 'Doctor'.
Also the Admin site I would like to see the only emp_type which are 'Doctor'.
Thanks
# You have to use filter for your query
emp_doctor = Employee.objects.filter(emp_type='Doctor')
print(emp_doctor)

django modelform how to know input type is checkbook?

My modelform is a dynamically generated modelform,I want to know the type of is_true in the modelForm. The type of the input tag is the checkbook type.
If I know the type=‘checkbox’ of the is_true field, add a class attr to him separately.
The default type='checkbox’ interface is too ugly
models
class Employee(AbstractBaseUser):
"""
用户表
"""
username = models.CharField(max_length=30, verbose_name='姓名')
email = models.EmailField(verbose_name='邮箱', unique=True)
is_true = models.BooleanField(default=False, verbose_name='是否超级用户')
views
class ModelFormDemo(ModelForm):
class Meta:
model = self.model
if self.list_editable:
fields = self.list_editable
else:
fields = '__all__'
excluded = self.excluded
def __init__(self, *args, **kwargs):
super(ModelFormDemo, self).__init__(*args, **kwargs)
def add_view(self, request):
form = ModelFormDemo()
if request.method == "POST":
res_dict = {'status': 1, 'msg': 'success'}
form = ModelFormDemo(request.POST)
if form.is_valid():
obj = form.save()
else:
res_dict['msg'] = form.errors
res_dict['status'] = 2
return JsonResponse(res_dict)
return render(request, "xadmin/add_view.html", locals())
html
<form class="layui-form" method="post">
{% csrf_token %}
{% for field in form %}
{% if field.name == 'employee' %}
<input type="hidden" name="employee" value="{{ user.id }}">
{% else %}
<div class="layui-form-item">
<label class="layui-form-label">{{ field.label }}</label>
<div class="layui-input-inline">
{{ field }}
</div>
</div>
{% endif %}
{% endfor %}
<div class="layui-form-item">
<div class="layui-input-block">
<input type="button" class="layui-btn" lay-filter="add" lay-submit="" value="add">
</input>
<button type="reset" class="layui-btn layui-btn-primary">reset</button>
</div>
</div>
</form>
You can use the Widget.attrs arg in your form __init__ method.
https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs

Creating a OneToOne Relationship between 2 models(forms) in the same view

I am building a signup page. I am using the default User model and a new model called UserInfo for additional user details. I need to establish a OneToOne relationship between the 2 models. To build the signup page, I made a form for each model and put them into the same signup view.
My question is, how to I set user(the OneToOneField) in UserInfo to the User that is being created in the same view UserInfo is created? If you look in my views.py below, immediately after I saved User's form(signup_form), I tried a few things like..
user2.user = User.objects.get(pk=pk) - doesn't seem to work
user2.user = request.user - doesn't work because request.user is an anonymous user in this case
user2.user = user - didn't work
models.py:
class UserInfo(models.Model):
TITLE = (
('Salesperson', 'Salesperson'),
('Sales Representative', 'Sales Representative'),
('Broker', 'Broker'),
('Broker of Record', 'Broker of Record'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
preferred_email = models.EmailField()
office_phone_number = models.CharField(max_length=10)
brokerage_of_agent = models.CharField(max_length=50)
agent_title = models.CharField(max_length=20, choices=TITLE)
def __str__(self):
return self.preferred_email
forms.py:
class SignupForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
class UserInfoForm(forms.ModelForm):
class Meta:
model = UserInfo
fields = ['preferred_email', 'office_phone_number', 'brokerage_of_agent', 'agent_title']
views.py:
def signup(request):
if request.user.is_authenticated:
return HttpResponseRedirect('../dashboard/')
if request.method == 'POST':
signup_form = SignupForm(request.POST)
basic_info_form = UserInfoForm(request.POST)
while (True): # The while loop is used to get access to 'break' for the #gmail Email check
if signup_form.is_valid():
if not signup_form.cleaned_data['email'].endswith('#gmail.com'):
print('You must register with your #gmail.com Email')
break
user = signup_form.save(commit=False)
user.is_active = False
user.save()
### Look here! I need to write something here I think, but I'm not sure what to write
user2 = basic_info_form.save(commit=False)
user2.user = User.objects.get(pk=pk)
user2 = user2.save()
current_site = get_current_site(request)
message = render_to_string('acc_active_email.html', {
'user':user,
'domain':current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
mail_subject = 'Activate your blog account.'
to_email = signup_form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
else:
signup_form = SignupForm()
basic_info_form = UserInfoForm()
return render(request, 'signup.html', {'signup_form': signup_form, 'basic_info_form': basic_info_form})
HTML Template:
{% extends "base.html" %}
{% load static %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in signup_form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="display: none">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
{% for field in basic_info_form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="display: none">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<button type="submit">Sign up</button>
</form>
{% endblock %}
user2.user = user was actually the correct answer, but something with the clumsy while loop screwed it up so I thought it didn't work.

Django Class Based View Form Won't Save Data To Database

I can not get my form data to commit to my sqlite3 database. I don't see any errors. I can commit data through admin, but not through my own controller using form. I've tried many diff. combos and still no success. I would like to use class based view, please. Everything works, the form just won't save the data to database. There are no errors.
url: url(r'^create/$', CreateRequest.as_view())
forms.py:
class CreateForm(ModelForm):
date_due = forms.DateTimeField(widget=widgets.AdminSplitDateTime)
class Meta:
model = Request
fields = ['region', 'user_assigned', 'user_requester', 'description']
views.py:
class CreateRequest(LoginRequiredMixin, CreateView):
model = Request
fields = ['region', 'user_assigned', 'user_requester', 'date_due', 'description']
template_name = "requests_app/createRequest.html"
form_class = CreateForm
success_url = '/'
def form_valid(self, form):
objects = form.save()
return super(CreateRequest, self).form_valid(form)
models.py:
class Request(models.Model):
region = models.ForeignKey(Region)
completed = models.BooleanField(default=False)
user_assigned = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='user_assigned')
user_requester = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='user_requester')
date_due = models.DateTimeField()
date_completed = models.DateTimeField(null=True, blank=True)
description = models.CharField(max_length=500)
objects = models.Manager()
open_requests = OpenRequests()
completed_requests = CompletedRequests()
def mark_completed(self):
if not self.completed:
self.completed = True
self.date_completed = datetime.datetime.now()
index.html:
<h1>hi</h1>
<form action="/create/" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.region.errors }}
<label for="id_region">Region</label>
{{ form.region }}
</div>
<div class="fieldWrapper">
{{ form.user_assigned.errors }}
<label for="id_user_assigned">User Assigned</label>
{{ form.user_assigned }}
</div>
<div class="fieldWrapper">
{{ form.user_requester.errors }}
<label for="id_user_requester">user_requester: </label>
{{ form.user_requester }}
</div>
<div class="fieldWrapper">
<p> {{ form.date_due.errors.as_text }} </p>
<label for="id_date_due">Due Date</label>
{{ form.date_due }}
</div>
<div class="fieldWrapper">
{{ form.description.errors }}
<label for="id_description">Descr.</label>
{{ form.description }}
</div>
<p><input type="submit" value="Submit Request" /></p>
{% if form.non_field_errors %}
{% for err in form%}
<div class="fieldWrapper">
<p class="form-error">{{ err }}</p>
<p class="form-error">{{ err.label_tag }} {{ field }}</p>
</div>
{% endfor %}
{% endif %}
</form>
{% endblock %}
in views.py you don't need this line: objects = form.save()
It can be
class ContaktCreateView(CreateView):
model = Contakt
form_class = ContaktForm
template_name = "www/www_contakt.html"
success_url = '/thanks/'
def form_valid(self, form):
return super(ContaktCreateView, self).form_valid(form)
Also I'm not using action in form action="/create/" method="post"
You are calling this html form via your line in urls.py:
url(r'^create/$', CreateRequest.as_view())
which is using your CreateRequest view which is using your index.html form file.

Using Check boxes in Taggit

I am trying to use Taggit for users to be able to tag their posts while they are submitting a form. But I can successfully let them type manually(their tags), I am trying to change to check boxes. Any ideas?
forms.py
class TalesForm(ModelForm):
class Meta:
model = Tale
fields = ('title', 'body', 'tags')
m_tags = TagField()
models.py
class Tale(models.Model):
title = models.CharField(max_length = 50)
body = models.TextField(max_length = 10000)
pub_date = models.DateTimeField(default = datetime.now)
poster = models.CharField(max_length = 30)
tags = TaggableManager()
def __unicode__(self):
return self.title
views.py
def add(request):
if request.user.is_authenticated():
if request.method=='POST':
form=TalesForm(request.POST)
if form.is_valid():
m_tags = form.cleaned_data['tags']
newTale=Tale(title=form.cleaned_data['title'], body=form.cleaned_data['body'], poster = request.user)
newTale.save()
for m_tag in m_tags:
newTale.tags.add(m_tag)
#form.save_m2m()
return HttpResponseRedirect('/home/%s'%newTale.id)
else:
return render_to_response('story/add.html', {'form':form}, context_instance=RequestContext(request))
else:
form=TalesForm()
args = {}
args.update(csrf(request))
args['form'] = form
context= {'form': form}
return render_to_response('story/add.html',context, context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/home')
html
<form method="post" action="/home/add/">
{% csrf_token %}
<div class="register_div">
{% if form.title.errors %}<p class="error" >{{ form.title.errors }}</p>{% endif %}
<p><label for="title"{% if form.title.errors %} class="error"{% endif %}>Title:</label></p>
<p>{{ form.title}}</p>
</div>
<div class="register_div">
{% if form.body.errors %}<p class="error" >{{ form.body.errors }}</p>{% endif %}
<p><label for="body"{% if form.body.errors %} class="error"{% endif %}>Body:</label></p>
<p>{{ form.body }}</p>
</div>
<div class="register_div">
<p><label for="tag">Tags:</label></p>
<p>{{ form.tags }}</p>
</div>
<input type="submit" value="Add Story" name="submit" />
</form>
Define your form this way:
from taggit.models import Tag
class TalesForm(ModelForm):
tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),
widget=forms.CheckboxSelectMultiple())
class Meta:
model = Tale
fields = ('title', 'body', 'tags')

Categories