I want chained dropdown list in model formset.
I have 4 models in my app App_Prod - Category, Product, OrderInfo, Order
In my form, I used two views combinely. OrderInfo and Order
So the choice list of product field of the Order model should be dependent on the category field of the OrderInfo model. Like if I select Electronics category the choice list should return only laptop and mobile option instead of showing all. See the image for your better understanding.
Please suggest me what should I edit in my codes, or is there any other way to do so.
models.py
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
def __str__(self):
return self.name
class OrderInfo(models.Model):
category = models.ForeignKey(Category, related_name='orderInfo', on_delete=models.CASCADE)
date = models.DateField()
def __str__(self):
return self.category.name
class Order(models.Model):
info = models.ForeignKey(OrderInfo, related_name='orders', on_delete=models.CASCADE)
product = models.ForeignKey(Product, related_name='productName', on_delete=models.CASCADE, null=True)
quantity = models.IntegerField()
def __str__(self):
return self.product.name
forms.py
class OrderInfoForm(forms.ModelForm):
date = forms.DateField(widget=DateInput)
class Meta:
model = OrderInfo
fields = ["category","date",]
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = ["product","quantity",]
widgets = {
'product': forms.Select(attrs={'class': 'formset-field form-control'}),
'quantity': forms.NumberInput(attrs={'class': 'formset-field form-control'}),
}
views.py
def order_create(request):
context = {}
OrderFormset = modelformset_factory(Order, form=OrderForm)
form = OrderInfoForm(request.POST or None)
formset = OrderFormset(request.POST or None, queryset=Order.objects.none(), prefix='productName')
if request.method == "POST":
if form.is_valid() and formset.is_valid():
try:
with transaction.atomic():
info = form.save(commit=False)
info.save()
for order in formset:
data = order.save(commit=False)
data.info = info
data.save()
except IntegrityError:
print("Error Encountered")
return redirect('App_Prod:order_create')
context['formset'] = formset
context['form'] = form
return render(request, 'App_Prod/order_create.html', context)
urls.py
urlpatterns = [
path('create/', views.order_create, name="order_create"),
]
order_create.html
<!DOCTYPE html>
{% extends 'base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block body_block %}
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<table class="table form-table table-bordered table-sm">
<thead class="text-center">
<tr>
<th>Product</th>
<th>Quantity</th>
<th></th>
</tr>
</thead>
<tbody>
{% for form_data in formset %}
<tr class="item">
<td>
{{ form_data.product }}
</td>
<td>
{{ form_data.quantity }}
</td>
<td>
<button type="button" class="btn btn-danger btn-sm remove-form-row"
id="{{ formset.prefix }}">
Delete
</button>
</td>
</tr>
{% endfor %}
<tr>
<td colspan="9"
style="border-left: none!important; border-right: none !important; border-bottom: none!important;">
<button type="button" class="btn btn-sm btn-success add-form-row"
id="{{ formset.prefix }}">
Add Activity
</button>
</td>
</tr>
</tbody>
</table>
{{ formset.management_form }}
<button class="btn btn-info" type="submit">Submit</button>
</form>
{% endblock %}
{% block extra_script %}
<script type="text/javascript" src="{% static 'js/formset.js' %}"></script>
{% endblock%}
Related
I would like when I add a file in the table it automatically adds a number from 1. I cannot use the primary key in the model because I already use it. anyone have a solution. thank you.
====Page.html====
<h5>Documents</h5>
</div>
<div class="row">
<div class="col-md">
<div class="card card-body">
<table class="table table-sm">
<tr>
<th>#</th>
<th>Nom du fichier</th>
<th>Fichier</th>
<th></th>
</tr>
{% for d in document %}
<tr>
<td>{{d.Number}}</td>
<td>{{d.Description}}</td>
<td><a download href="{{d.Fichier.url}}">{{d.Fichier}}</a></td>
<td><a class="btn btn-outline-danger" href="{% url 'supprimerdocument' d.id %}">Supprimer</a>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
====view.py====
# Button to add docs
#login_required(login_url='loginpage')
#allowed_users(allowed_roles=['admin', 'livraison'])
def createDocument(request):
forms = documentForm()
if request.method == 'POST':
forms = documentForm(request.POST, request.FILES)
if forms.is_valid():
forms.save()
return redirect('/employe')
context = {'forms':forms}
return render(request, 'accounts/document_form.html', context)
====Models.py====
class Document(models.Model):
employe = models.ForeignKey(Employe, null=True, on_delete=models.SET_NULL)
Number = models.IntegerField(default=1)
Description = models.CharField(max_length=100, null=True)
Fichier = models.FileField(upload_to='documents/')
data_created = models.DateTimeField(auto_now_add=True, null=True)
====Form.py====
class documentForm(ModelForm):
class Meta:
model = Document
fields = '__all__'
exclude = ['Number']
You can use forloop.counter in your template to access the (1-indexed) iteration of the for loop
{% for d in document %}
<tr>
<td>{{ forloop.counter }}</td>
...
</tr>
{% endfor %}
It looks like you can do an AutoField as per How do I make an auto increment integer field in Django?
But as a fallback: You could just do an Integer field and override the modelform's .save() method and auto fill it like self.some_field = Document.objects.all().count()
..if you do that, best to add a flag for if it's an edit form or not.. I believe you can do that with if self.instance: if true, is edit form
use callable default
def get_next_number():
max_val = Document.objects.aggregate(models.Max('Number'))['Number__max'] or 0
return max_val + 1
class Document(models.Model):
Number = models.IntegerField(default=get_next_number)
...
I am tasked with making a shopping crud project with models Products,categories,sub_categories,size,colors. Categories and subcategories are connected via foreign keys and I am using SERAILIZERS.the problem is that when I try to insert the data into sub Categories it doesnt come in both the database and the webpage
I also tried value = "{{c.category_name}}" as well in select dropdown as well
below are the models
class Categories(models.Model):
category_name = models.CharField(max_length=10)
category_description = models.CharField(max_length=10)
isactive = models.BooleanField(default=True)
class SUBCategories(models.Model):
category_name = models.ForeignKey(Categories,on_delete=models.CASCADE)
sub_categories_name = models.CharField(max_length=20)
sub_categories_description = models.CharField(max_length=20)
isactive = models.BooleanField(default=True)
show and insert function of sub_categories
def show_sub_categories(request):
showsubcategories = SUBCategories.objects.filter(isactive=True)
#print(showsubcategories)
serializer = SUBCategoriesSerializer(showsubcategories,many=True)
print(serializer.data)
return render(request,'polls/show_sub_categories.html',{"data":serializer.data})
def insert_sub_categories(request):
if request.method == "POST":
insertsubcategories = {}
insertsubcategories['sub_categories_name']=request.POST.get('sub_categories_name')
insertsubcategories['sub_categories_description']=request.POST.get('sub_categories_description')
form = SUBCategoriesSerializer(data=insertsubcategories)
if form.is_valid():
form.save()
print("hkjk",form.data)
messages.success(request,'Record Updated Successfully...!:)')
print(form.errors)
return redirect('sub_categories:show_sub_categories')
else:
print(form.errors)
else:
insertsubcategories = {}
form = SUBCategoriesSerializer(data=insertsubcategories)
category_dict = Categories.objects.filter(isactive=True)
category = CategoriesSerializer(category_dict,many=True)
hm = {'context': category.data}
if form.is_valid():
print(form.errors)
return render(request,'polls/insert_sub_categories.html',hm)
html of the insert page and show page respectively
<td>category name</td>
<td>
<select name="category_name" id="">
{% for c in context %}
<option value="{{c.id}}">{{c.category_name}}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td>sub categories Name</td>
<td>
<input type="text" name="sub_categories_name" placeholder="sub categories ">
</td>
</tr>
<tr>
<td>Sub categories Description</td>
<td>
<textarea name="sub_categories_description" id="" cols="30" rows="10">
</textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Insert" />
</td>
<td>
{% if messages %}
{% for mess in messages %}
<b style="color:green;">{{mess}}</b>
{% endfor %}
{% endif %}
</td>
<tbody>
<tr>
<td><b>{{result.sub_categories_name}}</b></td>
<td><b>{{result.sub_categories_description}}</b></td>
<td style="position: relative;left:50px;">
<a href="sub_categories/edit_sub_categories/{{result.id}}">
<button class="btn btn-primary">
<i class="fa-solid fa-pen-to-square">EDIT</i>
</button>
</a>
</td>
<td>
<a href="{% url 'sub_categories:delete_sub_categories' result.id %}" onclick="return confirm('Are You Sure you want to delete?')">
<button class="btn btn-danger">
<i class="fa-solid fa-trash">DELETE</i>
</button>
</a>
</td>
</tr>
</tbody>
categories and sub categories serializer
class CategoriesSerializer(serializers.ModelSerializer):
class Meta:
model = Categories
fields = "__all__"
extra_kwargs = {'category_name': {'required': False}}
class SUBCategoriesSerializer(serializers.ModelSerializer):
class Meta:
model = SUBCategories
fields = "__all__"
where am I going wrong in the code?
Try this out and let me know, a few changes you'd require
def insert_sub_categories(request):
if request.method == "POST":
form = SUBCategoriesSerializer(data=request.POST)
if form.is_valid():
form.save()
messages.success(request, "Record Updated Successfully...!:)")
return redirect("sub_categories:show_sub_categories")
category_dict = Categories.objects.filter(isactive=True)
category = CategoriesSerializer(category_dict, many=True)
hm = {"context": category.data}
return render(request, "polls/insert_sub_categories.html", hm)
Form part
<form method="POST">
{% csrf_token %}
<table>
<thead>
<tr>
<td>category name</td>
<td>
<select name="category_name" id="">
{% for c in context %}
<option value="{{c.id}}">{{c.category_name}}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td>sub categories Name</td>
<td>
<input type="text" name="sub_categories_name" placeholder="sub categories ">
</td>
</tr>
<tr>
<td>Sub categories Description</td>
<td>
<textarea name="sub_categories_description" id="" cols="30" rows="10">
</textarea>
</td>
</tr>
<tr>
<td>Active</td>
<td>
<input type="checkbox" name="isactive" value="true" checked>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Insert" />
</td>
<td>
{% if messages %}
{% for mess in messages %}
<b style="color:green;">{{mess}}</b>
{% endfor %}
{% endif %}
</td>
</tr>
</thead>
</table>
<button class="btn btn-success">Go To Home</button>
</form>
model
class SUBCategories(models.Model):
category_name = models.ForeignKey(Categories, on_delete=models.CASCADE)
sub_categories_name = models.CharField(max_length=20)
sub_categories_description = models.CharField(max_length=20)
isactive = models.BooleanField(default=True)
def __str__(self):
return self.category_name.category_name
This error occurs because there is a chance in your function view, that it does not return proper Response or Redirect object. If you send POST request, but the form is not valid, then it will not return anything.
def insert_sub_categories(request):
if request.method == "POST":
...
if form.is_valid():
...
return redirect('sub_categories:show_sub_categories')
else:
print(form.errors)
# HERE YOU MISS REDIRECT/RENDER
else:
...
return render(request,'polls/insert_sub_categories.html',hm)
Also you don't pass SUBCategories.category_name to your serializer. It's required field, so it will never be valid without it. You may try form = SUBCategoriesSerializer(request.POST) instead of adding values one by one.
also any advise you on how to design the reply model is welcomed.
or even how to better structure the views
The items are created under one parent through a formset
what am trying to archive
unit_price, item_total_price are associated with a single item
delivery_charges , overall_total, subtotal are associated with all the items models,
class RfqFullPrice(models.Model):
sender = models.name = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="quote_sender")`
receiver = models.name = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="quote_reciever")
requestedquotation = models.ForeignKey(RequestForQuotation, related_name="replyprice_rfq", on_delete=models.CASCADE)
subtotal = MoneyField(max_digits=14, decimal_places=2, default_currency='USD', validators=[MinMoneyValidator(0)])
delivery_charges = MoneyField(max_digits=14, decimal_places=2, default_currency='USD', validators=[MinMoneyValidator(0)])
overall_total = MoneyField(max_digits=14, decimal_places=2, default_currency='USD', validators=[MinMoneyValidator(0)])
validity_start_date = models.DateField()
validity_end_date = models.DateField()
class ReplyItemPrice(models.Model):
full_price = models.ForeignKey(RfqFullPrice, on_delete=models.CASCADE, related_name='full_price')
the_rfq = models.ForeignKey(RequestForQuotation, on_delete=models.CASCADE)
item = models.ForeignKey(RequestForQuotationItem, on_delete=models.CASCADE, related_name="rfq_item")
quantity = models.CharField(max_length = 200, help_text='Enter the qauntity')
unit_price = MoneyField(max_digits=14, decimal_places=2, default_currency='USD', validators=[MinMoneyValidator(0)])
item_total_price = MoneyField(max_digits=14, decimal_places=2, default_currency='USD', validators=[MinMoneyValidator(0)])
def quote(request, pk):
template_name = 'request_quote/send_quote.html'
requestedquotation = get_object_or_404(RequestForQuotation, pk=pk)
items = RequestForQuotationItem.objects.filter(item=requestedquotation)
if request.method == 'GET':
full_priceform =QuoteFullPriceForm()
item_priceform = QuoteItemPriceForm()
elif request.method == 'POST':
full_priceform =QuoteFullPriceForm(request.POST, request.FILES)
item_priceform = QuoteItemPriceForm(request.POST, request.FILES)
if full_priceform.is_valid() and item_priceform.is_valid():
full_priceform = full_priceform.save(commit=False)
full_priceform.receiver = requestedquotation.sender
full_priceform.sender = request.user
full_priceform.requestedquotation = requestedquotation
full_priceform.save()
for form in item_priceform:
form = item_priceform.save(commit=False)
form.full_price = full_priceform
form.the_rfq = requestedquotation
form.item = items
form.save()
messages.success(request, 'Uploaded'\
'successfully')
return redirect('/')
else:
messages.error(request, 'Error While Uploading')
context = {
'full_priceform':full_priceform,
'item_priceform':item_priceform,
'requestedquotation':requestedquotation,
'items':items,
}
return render(request, template_name, context)
<form class="form-horizontal" method="POST" action="{% url 'rfq:quote' requestedquotation.pk %}" enctype="multipart/form-data">
{% csrf_token %}
{% for item in items %}
<tr>
<td align="center"><span class="badge badge-primary">#{{ item.pk }}</span></td>
<td align="center">{{ item.name }}</td>
<td align="center">{{ item.discription|safe|linebreaksbr }}</td>
<td align="center"><img src="{% if item.image %} {{ item.image.url }} {% else %} {% static 'img/default.jpg' %} {% endif %}" alt="..." height="150" width="150"></td>
<td align="center">{{ item.quantity }}</td>
<td align="center">{{ item_priceform }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ full_priceform.subtotal }}
{{ full_priceform.delivery_charges }}
{{ full_priceform.overall_total }}
{{ full_priceform.validity_start_date }}
{{ full_priceform.validity_end_date }}
<button type="submit" class="btn btn-success mb-2">Submit</button>
</form>
I'm getting this error using Django 3 when trying to either 1) create a new object (CreateView), or 2) edit an existing one (UpdateView). Using CBVs and ModelForms.
I explored similar questions on Stackoverflow. Very often, it's about the template missing pk reference, but I triple-checked that and it doesn't seem to be the case.
Any idea?
Here are my codes:
URLs
path('availability/<int:pk>/list', views.AvailabilityListView.as_view(), name='availability_list'),
path('availability/new/', views.AvailabilityCreateView.as_view(), name='availability_new'),
path('availability/<int:pk>/edit/', views.AvailabilityUpdateView.as_view(), name='availability_edit'),
Views
class AvailabilityUpdateView(UpdateView):
template_name = 'crm/availability_form.html'
form_class = AvailabilityForm
model = Availability
class AvailabilityUpdateView(UpdateView):
template_name = 'crm/availability_form.html'
form_class = AvailabilityForm
model = Availability
class AvailabilityListView(TemplateView):
template_name = 'crm/availability_list.html'
def get_context_data(self, **kwargs):
user = self.request.user
kwargs['availabilities'] = Availability.objects.filter(staff__manager=Manager.objects.get(user=user)).filter(staff=self.kwargs['pk'])
return super().get_context_data(**kwargs)
Forms
class AvailabilityForm(forms.ModelForm):
class Meta():
model = Availability
exclude = []
Models
class Availability(models.Model):
staff = models.ForeignKey(Staff, on_delete=models.CASCADE)
start = models.DateTimeField()
end = models.DateTimeField()
class Meta:
verbose_name_plural = "Availabilities"
def get_absolute_url(self):
return reverse('staff_list')
Template
{% block body_block %}
<div class="container">
<div class="heading">
<h1>Availability</h1>
<a class = 'btn btn-primary' href="{% url 'availability_new' %}">+Create new availability</a>
</div>
<hr/>
{% if availabilities %}
<table class="table">
<tbody>
{% for availability in availabilities %}
<tr>
<td>{{availability.pk}}</td>
<td>{{availability.staff}}</td>
<td>{{availability.start}}</td>
<td>{{availability.end}}</td>
<td>
Edit
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p class='empty'>Staff does not have any availability</p>
{% endif %}
</div>
{% endblock %}
my check in is as automatic and my checkout as None, and I wanted the page to update register inside the html, I put a button checked and when I click on it that the checkout was updated with the current time and it was already filled in the form, without needing leave the page
I tried and I can not make the mistake
Model.py
PAGO_CHOICES = (
('Não', 'Não Pago'),
('Sim', 'Pago')
)
class MovRotativo(models.Model):
checkin = models.DateTimeField(auto_now=True, blank=False, null=False,)
checkout = models.DateTimeField(default=None, null=True, blank=True)
email = models.EmailField(blank=False)
placa = models.CharField(max_length=7, blank=False)
modelo = models.CharField(max_length=15, blank=False)
valor_hora = models.DecimalField(
max_digits=5, decimal_places=2, null=False, blank=False)
pago = models.CharField(max_length=15, choices=PAGO_CHOICES)
views.py
#login_required
def movrotativos_update(request, id):
data = {}
mov_rotativo = MovRotativo.objects.get(id=id)
form = MovRotativoForm(request.POST or None, instance=mov_rotativo)
data['mov_rotativo'] = mov_rotativo
data['form'] = form
if request.method == 'POST':
if form.is_valid():
form.save()
return redirect('core_lista_movrotativos')
else:
return render(request, 'core/update_movrotativos.html', data)
html.
{%extends 'basenew.html' %}
{% load bootstrap %}
{% block main %}
<main role="main" class="col-md-12 ml-sm-auto col-lg-10 px-50">
<br>
<br><br>
<div class="row">
<div class="col">
<h2>UpDating MovRotativos: {{ mov_rotativo}}</h2>
<form action="{% url 'core_movrotativos_update' mov_rotativo.id %}"
method="POST">
{% csrf_token %}
{{form|bootstrap}}
<table class="table table-bordered sortable" >
<thead class="p-3 mb-2 bg-primary text-white">
<tr>
<th scope="col">Horas</th>
<th scope="col">Pagar</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="" style="color:black">
{{mov_rotativo.horas_total}}</a></td>
<td><a href="" style="color:black">
{{mov_rotativo.total}}</a></td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-
primary">Salvar</button>
<form action="{% url 'core_movrotativos_checkout'
mov_rotativo.id %}" method="POST">
<button type="submit" class="btn btn-
primary">Checkout</button>
</form>
<a href="{% url 'core_movrotativos_delete' mov_rotativo.id%}"
class="btn btn-danger">Delete</a>
</form>
</div>
</div>
</main>
{% endblock %}