How to convert these loops into fine optimized Django queries - django

Could you, please help me optimizing this view? It's takes more than 30 minutes to accomplish the job.
I think the problem is in the Django queries.cSomeone recommended me to put all queries in a list or dictionary
I tried it but I don't see how to deduce the Django queries.
here are node and edge models
class Edge(models.Model):
geometry = models.LineStringField(null=True)
name = models.CharField(max_length=200, blank=False)
target = models.ForeignKey(Node, on_delete=models.CASCADE)
source = models.ForeignKey(Node, on_delete=models.CASCADE)
network = models.ForeignKey(RoadNetWork,
on_delete=models.CASCADE)
road_type = models.ForeignKey(RoadType,
on_delete=models.CASCADE)
class Node(models.Model):
network = models.ForeignKey(RoadNetWork,
on_delete=models.CASCADE)
node_id = models.BigIntegerField()
name = models.CharField('Node Name', max_length=200)
location = models.PointField()
def upload_edge(request, pk):
template = "networks/edge.html"
roadnetwork = RoadNetWork.objects.get(id=pk)
road_type = RoadType.objects.get(pk=2)
_node_instance_ = Node.objects.select_related('network').filter(network_id=pk)
if request.method == 'POST':
form = EdgeForm(request.POST, request.FILES)
if form.is_valid():
datafile = request.FILES['my_file']
objects = json.load(datafile)
L=[]
for object in objects['features']:
objet_type = object['geometry']['type']
if objet_type == 'LineString':
properties = object['properties']
geometry = object['geometry']
point1 = geometry['coordinates'][0]
point2 = geometry['coordinates'][1]
location = GEOSGeometry(
LineString(geometry['coordinates']))
target = properties.get('target')
source = properties.get('source')
name = properties.get('name')
try:
target = _node_instance_.get(node_id=target)
source = _node_instance_.get(node_id=source)
node = Edge(
geometry=location,
road_type=road_type,
target=target,
source=source,
network=roadnetwork)
L.append(node)
except:
pass
Edge.objects.bulk_create(L)
return redirect('home')
else:
form = EdgeForm()
return render(request, template, {'form': form})

Related

django form commit=false after how to save many to many field data

Model.py
class Branch(models.Model): # Branch Master
status_type = (
("a",'Active'),
("d",'Deactive'),
)
name = models.CharField(max_length=100, unique=True)
suffix = models.CharField(max_length=8, unique=True)
Remark = models.CharField(max_length=200, null=True, blank=True)
created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=1, choices = status_type, default = 'a')
def __str__(self):
return self.name
class Vendor(models.Model):
status_type = (
("a",'Active'),
("d",'Deactive'),
)
branch = models.ManyToManyField(Branch)
company = models.CharField(max_length=200)
name = models.CharField(max_length=200)
phone = models.CharField(max_length=11, unique = True)
email = models.EmailField(max_length=254, unique = True)
gst = models.CharField(max_length=15, unique = True)
pan_no = models.CharField(max_length=10, unique = True)
add_1 = models.CharField(max_length=50, null=True, blank = True)
add_2 = models.CharField(max_length=50, null=True, blank = True)
add_3 = models.CharField(max_length=50, null=True, blank = True)
Remark = models.CharField(max_length=200, null=True, blank=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=1, choices = status_type, default = 'a')
def __str__(self):
return self.company
form.py
i want save like created_by field
class VendorForm(ModelForm):
class Meta:
model = Vendor
fields = 'all'
exclude = ['created_by', 'branch']
widgets = {
'company':forms.TextInput(attrs={'class':'form-control'}),
'name':forms.TextInput(attrs={'class':'form-control'}),
'phone':forms.TextInput(attrs={'class':'form-control'}),
'email':forms.EmailInput(attrs={'class':'form-control'}),
'gst':forms.TextInput(attrs={'class':'form-control'}),
'pan_no':forms.TextInput(attrs={'class':'form-control'}),
'add_1':forms.TextInput(attrs={'class':'form-control'}),
'add_2':forms.TextInput(attrs={'class':'form-control'}),
'add_3':forms.TextInput(attrs={'class':'form-control'}),
'Remark':forms.Textarea(attrs={'class':'form-control','rows':'2'}),
'status':forms.Select(attrs={'class':'form-control'}),
}
Views.py
I have pass branch in session.
I want to save with branch which is many to many field
def Add_Vendor(request): # for vendor add
msg = ""
msg_type = ""
branch_id = request.session['branch_id']
branch_data = Branch.objects.get(id = branch_id)
form = ""
if request.method == "POST":
try:
form = VendorForm(request.POST)
if form.is_valid:
vendor_add = form.save(commit=False)
vendor_add.created_by = request.user
vendor_add.instance.branch = branch_data.id
vendor_add.save()
form.save_m2m() # for m to m field save
msg_type = "success"
msg = "Vendor Added."
form = VendorForm(initial={'branch':branch_id})
except:
msg_type = "error"
msg = str(form.errors)
print(msg)
else:
form = VendorForm(initial={'branch':branch_id})
context = {
'form':form,
'branch_data':branch_data,
'msg_type':msg_type,
'msg':msg,
'btn_type':'fa fa-regular fa-plus',
'form_title':'Vendor Form',
'tree_main_title':'Vendor',
'v_url':'vendor_page',
'tree_title':'Add Form',
}
return render(request, 'base/vendor_master/form_vendor.html',context)
I would advise not to work with commit=False in the first place:
def Add_Vendor(request): # for vendor add
branch_id = request.session['branch_id']
branch_data = get_object_or_404(Branch, pk=branch_id)
if request.method == 'POST':
form = VendorForm(request.POST, request.FILES)
if form.is_valid():
form.instance.created_by = request.user
form.instance.branch = branch_data.id
vendor_add = form.save()
vendor_add.branch.add(branch_data)
return redirect('name-of-some-view')
else:
form = VendorForm()
context = {
'form': form,
'branch_data': branch_data,
'btn_type': 'fa fa-regular fa-plus',
'form_title': 'Vendor Form',
'tree_main_title': 'Vendor',
'v_url': 'vendor_page',
'tree_title': 'Add Form',
}
return render(request, 'base/vendor_master/form_vendor.html', context)
You can simplify your form by automatically adding form-control to each widget:
class VendorForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
attrs = field.widget.attrs
attrs['class'] = attrs.get('class', '') + ' form-control'
class Meta:
model = Vendor
exclude = ['created_by', 'branch']
Note: In case of a successful POST request, you should make a redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
Note: You can set a field editable=False [Django-doc]. Then the field does not show up in the ModelForms and ModelAdmins by default. In this case for example with created_by.
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
Note: Please do not pass messages manually to the template. Django has the messages framework [Django-doc], which allows to add messages to the request, which will then be delivered the next time a template renders these messages. This makes delivering multiple messages convenient, as well as setting different log levels to the messages.

select filtering and removal if they are already present in the db

look at the picture before answering me.
that group2 is inside saved in the db with the button I open a modal that allows me to save other groups in the db and I would like that the same groups no longer appear in that select if I have already added them
form.py
class EserciziForm(forms.ModelForm):
class Meta:
model = models.DatiEsercizi
exclude = ['gruppo_single']
#fields = '__all__'
class GruppiForm(forms.ModelForm):
class Meta:
model = models.DatiGruppi
exclude = ['gruppi_scheda']
views.py
def creazione(request, nome):
scheda = get_object_or_404(Schede, nome_scheda = nome)
eserciziFormSet = formset_factory(EserciziForm, extra = 0)
if request.method == "POST":
gruppo_form = GruppiForm(request.POST, prefix = 'gruppo')
if gruppo_form.is_valid():
gruppo = gruppo_form.save(commit = False)
gruppo.gruppi_scheda = scheda
gruppoName = gruppo_form.cleaned_data['dati_gruppo']
gruppo.save()
esercizi_formset = eserciziFormSet(request.POST, prefix='esercizi')
for esercizi in esercizi_formset:
esercizi_instance = esercizi.save(commit = False)
esercizi_instance.gruppo_single = get_object_or_404(DatiGruppi, gruppi_scheda = scheda.id, dati_gruppo = gruppoName)
esercizi_instance.save()
return HttpResponseRedirect(request.path_info)
else:
gruppo_form = GruppiForm(prefix = 'gruppo')
esercizi_formset = eserciziFormSet(prefix='esercizi')
context = {'scheda' : scheda, 'gruppo_form' : gruppo_form, 'esercizi_formset': esercizi_formset}
return render(request, 'crea/passo2.html', context
models.py
class DatiGruppi(models.Model):
giorni_settimana_scelta = [
("LUNEDI","Lunedì"),
("MARTEDI","Martedì"),
("MERCOLEDI","Mercoledì"),
("GIOVEDI","Giovedì"),
("VENERDI","Venerdì"),
("SABATO","Sabato"),
("DOMENICA","Domenica")
]
giorni_settimana = MultiSelectField(choices = giorni_settimana_scelta,default = '-')
dati_gruppo = models.ForeignKey(
Gruppi,on_delete = models.CASCADE, related_name = 'dati_gruppo')
gruppi_scheda = models.ForeignKey(Schede,on_delete = models.CASCADE, related_name = 'gruppi_scheda')
class Schede(models.Model):
nome_scheda = models.CharField(max_length=100)
data_inizio = models.DateField()
data_fine = models.DateField()
utente = models.ForeignKey(User, on_delete = models.CASCADE,related_name = 'utente')
You can override a form field before instantiate it like this :
views.py
from django import forms
if request.method == "POST":
# Post logic here
else:
# We try to retrieve group that the current user is not yet in.
# Not your logic, but to sum up, you have to retrieve the groups
# which had not yet been added.
# Use a filter that permit you to retrieve only groups which had not yet been added.
group_to_add = Group.objects.filter(...)
GruppiForm.base_fields['group_field'] = forms.ModelChoiceField(
queryset=group_to_add)
# Instantiate the form now
# In this form, the choices are only those contained in the group_to_add queryset
form = GruppiForm(prefix = 'gruppo')

Django changing models field into views

I everyone, I have a problem with a django's view. My goal is to change the 'execute' field into 'True' if newOrder is buy and there is some other sell order with a inferior price. And reverse for sell newOrders. I want to change the 'execute' field for the newOrder and also for the other order (in pairs). That's my code:
views.py
def order(request):
form = OrderForm()
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
new_order = form.save()
if new_order.buy is True:
sellOrder = Order.objects.filter(sell=True, execute=False,
price__lte=new_order.price).first().update(execute=True)
new_order.execute = True
sellOrder.save()
else:
buyOrder = Order.objects.filter(buy=True,
execute=False,price__gte=new_order.price).first().update(execute=True)
new_order.execute = True
buyOrder.save()
new_order.profile = request.user
new_order.save()
return redirect('home')
else:
form = OrderForm()
contex = {'form': form}
return render(request, 'app/new_order.html', contex)
models.py
class Profile(models.Model):
_id = ObjectIdField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
wallet = models.FloatField()
class Order(models.Model):
_id = ObjectIdField()
profile = models.ForeignKey(User, on_delete=models.CASCADE)
datetime = models.DateTimeField(auto_now_add=True)
buy = models.BooleanField(blank=True)
sell = models.BooleanField(blank=True)
price = models.FloatField()
quantity = models.FloatField()
execute = models.BooleanField(blank=True)
But something goes wrong. This is the error:
AttributeError at /new_order/
'NoneType' object has no attribute 'update'
sellOrder returns a count of updated rows, not the object updated
sellOrder = Order.objects.filter(sell=True, execute=False,
price__lte=new_order.price).first().update(execute=True)
instead try
sellOrder = Order.objects.filter(sell=True, execute=False,
price__lte=new_order.price).first()
new_order.execute = True
sellOrder.execute = True
sellOrder.save()

Multi-file upload using Django not working as expected

Im having trouble on uploading multiple files. The uploaded files are pdf and only one is saved.
views.py
def attachments(request):
to = TravelOrder.objects.order_by('-date_filling').last()
if request.method == 'POST':
form = AttachmentsForm(request.POST, request.FILES)
if form.is_valid():
for f in request.FILES.getlist('attachment'):
file_instance = Attachements(travel_order=to, attachment=f)
file_instance.save()
print('YEY')
return redirect('attach')
else:
form = AttachmentsForm()
context = {
'form': form
}
return render(request, 'employee/attachments.html', context)
models.py
class TravelOrder(models.Model):
created_by = models.CharField(max_length=255)
start_date = models.DateField(auto_now=False)
end_date = models.DateField(auto_now=False)
wfp = models.CharField(max_length=255, verbose_name='Wfp Where to be charged')
purpose_of_travel = models.CharField(max_length=255)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
venue = models.CharField(max_length=255)
date_filling = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=15)
def __str__(self):
return self.purpose_of_travel
class Attachements(models.Model):
at_id = models.AutoField(primary_key=True)
travel_order = models.ForeignKey(TravelOrder, on_delete=models.CASCADE)
attachment = models.FileField(upload_to='attachment/')
I think you are returing before the loop finishes.
for f in request.FILES.getlist('attachment'):
file_instance = Attachements(travel_order=to, attachment=f)
file_instance.save()
print('YEY')
return redirect('attach')
Take the return outside loop, so that the redirect happens only after saving all files. That means
for f in request.FILES.getlist('attachment'):
file_instance = Attachements(travel_order=to, attachment=f)
file_instance.save()
print('YEY')
return redirect('attach')

How to fill empty fields when I save form from POST

I try to fill empty fields when I save model form, but unfortunately, it doesn't work properly.
this is my view:
def bylaw_save(request):
form = BylawForm(initial={'who_created': request.user.username})
if request.method == 'POST':
form = BylawForm(request.POST)
if form.is_valid():
gdn = GlobalDocNumber.objects.get(pk=1)
gdn.gdn += 1
gdn.save()
raspr_num = form.cleaned_data['raspr_num'][:]
raspr_num = raspr_num.split('/')
bylaw = form.save(commit=False)
bylaw.district = 'unfilled'
bylaw.department = 'unfilled'
bylaw.performer = 'unfilled'
bylaw.check_type = 'unfilled'
bylaw.save()
return bylaw_form(request, msg='test')
else:
return bylaw_form(request, msg='test')
return render(request, 'bylaw/bylaw_form.html', {'form': form})
this is fraction of my form:
district = ModelChoiceField(required=False, queryset=DistrictsMenu.objects.all(), to_field_name="district",
widget=Select(attrs={'id': "district", 'class': 'form-control col-6'}))
department = ModelChoiceField(required=False, queryset=DepartmentsMenu.objects.all(), to_field_name="department",
widget=Select(attrs={'id':"department", 'class': "form-control col-6"}))
UPDATE: This is my model with default='Unfilled', according to Arthur M and Rohan suggestions (But it also doesn't work, it gives me a "NOT NULL constraint failed: bylaw_bylawmodel.department" error, in this case I don't fill department field):
class BylawModel(models.Model):
raspr_date = models.DateField()
district = models.CharField(max_length=255, default='Unfilled')
department = models.CharField(max_length=255, default='Unfilled')
organization = models.CharField(max_length=255, default='Unfilled')
inn = models.IntegerField()
performer = models.CharField(max_length=255, default='Unfilled')
check_type = models.CharField(max_length=255)
date_proved = models.DateField()
raspr_num = models.CharField(max_length=255, unique=True)
who_created = models.CharField(max_length=255)
when I save this from, it always fills with 'unfilled'. How can I fill empty values only if they are really empty?
By doing
bylaw.fieldName = 'unfilled' you're overwriting the value sent from your form.
If you don't want to add a default value in your model (which I would recommend) you can add a simple :
if not bylaw.fieldName:
bylaw.fieldName = 'unfilled'
For every one of your fields.